【CodeForces 574B】Bear and Three Musketeers
【链接】  我是链接,点我呀:) 
 【题意】
【题解】
枚举每一条边(x,y)
然后再枚举y的出度z
看看g[x][z]是否等于1(表示联通)
如果等于1就说明找到了一个三元环,则尝试用它们的出度和-6更新答案就好。
时间复杂度O(M*N)
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;
const int N = 4e3;
int n,m;
vector<int> g[N+10];
bool G[N+10][N+10];
vector<pair<int,int> > v;
int main()
{
    #ifdef LOCAL_DEFINE
        freopen("rush.txt","r",stdin);
    #endif // LOCAL_DEFINE
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> m;
    rep1(i,1,m){
        int x,y;
        cin >> x >> y;
        v.push_back({x,y});
        G[x][y] = G[y][x] = 1;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int ans = -1;
    for (int i = 0;i < m;i++){
        int x = v[i].first,y = v[i].second;
        for (int j = 0;j < (int) g[y].size();j++){
            int z = g[y][j];
            if (G[x][z]){
                int temp = g[x].size();
                temp+=g[y].size();
                temp+=g[z].size();
                temp-=6;
                if (ans==-1){
                    ans = temp;
                }else{
                    ans = min(ans,temp);
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
												
											【CodeForces 574B】Bear and Three Musketeers的更多相关文章
- 【32.89%】【codeforces 574D】Bear and Blocks
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 - 【codeforces 791D】 Bear and Tree Jumps
		
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
 - 【codeforces 791C】Bear and Different Names
		
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
 - 【codeforces 791B】Bear and Friendship Condition
		
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
 - 【codeforces 791A】Bear and Big Brother
		
[题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...
 - 【19.05%】【codeforces 680D】Bear and Tower of Cubes
		
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - 【Codeforces 385C】Bear and Prime Numbers
		
[链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...
 - 【Codeforces 639B】Bear and Forgotten Tree 3
		
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
 - 【Codeforces 639A】Bear and Displayed Friends
		
[链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...
 
随机推荐
- 《编程导论(Java)·2.1.2 啊,我看到了多态》-什么是多态(polymorphism)
			
1.不明觉厉 很多人学习多态时,会认为. 之所以不明觉厉,由于多态的定义:事物存在的多种表现形态:而后,有人将重载(overload).改写(override).多态变量和泛型归结于同一个术语&quo ...
 - 64位win7中使用vs2013为python3.4安装pycrypto-2.6.1插件报Unable to find vcvarsall.bat异常解决方式
			
问题描写叙述: 64位win7中使用vs2013为python3.4.2安装pycrypto-2.6.1插件报Unable to find vcvarsall.bat. 问题分析: 1.源代码分析,查 ...
 - Struts简单介绍
			
一.在介绍struts之前,先来了解一下什么是MVC框架吧. 1.MVC介绍 MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controlle ...
 - nginx源代码分析--进程间通信机制 & 同步机制
			
Nginx源代码分析-进程间通信机制 从nginx的进程模型能够知道.master进程和worker进程须要通信,nginx中通信的方式有套接字.共享内存.信号.对于master进程,从外部接受信号, ...
 - vim分屏功能
			
转载,来自http://coolshell.cn/articles/1679.html 本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On fi ...
 - FluentScheduler定时器
			
项目需要一个按时执行的任务,每隔几分钟执行一个,或者每隔几小时执行一次等等,这个时候就需要一个定时的功能,最简单的就是用Timer自己写一个,但是自己写的性能等各方面有可能不健全等等,而现在开源的库也 ...
 - C - Ilya and Sticks(贪心)
			
Problem description In the evening, after the contest Ilya was bored, and he really felt like maximi ...
 - guice基本使用,三种注入方式(二)
			
guice提供了强大的注入方式. 1.属性注入 2.构造器注入 3.set方式注入 1.属性注入: package com.ming.user.test; import com.google.inje ...
 - eclipse中文汉字看不清或过小的问题解决方法!!
			
把字体修改为 中欧字体就可以看清汉字
 - 试图ddms 如果丢失adv链接解决办法!
			
点击如下图菜单 重启链接adv即可显示.