【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. 保证这个数组是有序的. 写个 ...
随机推荐
- impdp时卡住,DW等待library cache lock
同事反映impdp时在SCHEMA_REPORT/TYPE/TYPE_SPEC步骤卡住,1个多小时后也没有响应, 查下v$session: select program,sid, event,bloc ...
- <图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频
使用UIImagePickerController 进行录制 #import "ViewController.h" #import <MobileCoreServices/M ...
- 【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)
所实现的循环单链表的结构例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill ...
- python使用pytest+pytest报告
需要安装pytest和pytest-html pip3 install -U pytest pip3 install -U pytest-html
- windows XP下Python2.7包管理工具安装-setuptool,pip、distribute、nose、virtualenv
在Python开发中为了对项目进行管理和调试.必须安装一些特定的软件包.据说业内这个叫做yak shaving-做一个非常酷非常绚丽的Python项目之前,必须做的一些枯燥无味的准备工作.本文介绍了s ...
- Web前端开发实战6:CSS实现导航菜单结合二级下拉式菜单的简单变换
前面几篇博文都在讲导航菜单和二级下拉式菜单,事实上有非常多方法都能够实现的.详细的情况还要视情况而定. 在后面学习到jQuery框架之后,会有更丰富的动画效果.因为在学习Ajax和jQuery的初步阶 ...
- 用SecureCRT在linux系统下载文件
使用sz命令 说明如下: sz --helpsz version 0.12.20Usage: sz [options] file ... or: sz [options] -{c|i} COMMA ...
- Ubuntu下安装sublime text3并汉化
转载请注明出处:果冻栋吖 通过ppa安装,打开终端,输入以下命令: sudo add-apt-repository ppa:webupd8team/sublime-text- sudo apt-get ...
- 9.14[XJOI] NOIP训练33
今日9.14 洛谷打卡:大凶!!!(换个字体玩玩qwq) -------------------------------------------------------- 一个超颓的上午 今天又是fl ...
- SQL学习整理
SQL整理 SQL 对大小写不敏感! 一.对数据的操作 实现功能分类: 1. 增: 1.1 表存在,插入栏位: //插入新的行(按栏位的顺序插入) INSERT INTO Table_1 VALUES ...