Gang Black and Gang White (二分图染色法)
2017-06-23 19:34
357 查看
Problem: Gang Black and Gang White
Problem DescriptionThere are only two gangs in the city, Gang Black and Gang White. There are N persons(2<=N<=50000) who belong to these two gangs(one person only belongs to one gang). They are numbered from 1 to N.
As a policeman, you are given M pieces of messages. Each message has two integers A and B. It means that person A and person B are not in the
same gang.
Now you have to judge whether there is something wrong with these M pieces of messages. If you find that there is someone belongs to these two gangs at the same time through these messages, you will think there
is something wrong with these messages.
Input
The first line contains an integer T(1<=T<=20),the number of cases.
Then T cases follow. Each test case begins with a line with two integers N and M(0<=M<=50000). And then, each of the following M messages contains two integers A and B per line. (1<=A,B<=N)
Output
The output for every test case is a line containing "Test case #i:", where i is the number of the test case starting at 1, followed by one line saying either "Something wrong!" if you find out a man belongs to two
gangs in the same time, or "Nothing special." if you don't find something wrong.
There is a blank line after each test case.
Sample Input
34 22 14 33 31 21 32 35 11 2
Sample Output
Test case #1:
Nothing special.
Test case #2:
Something wrong!
Test case #3:
Nothing special.
题意:
告诉你有n个人和m条消息,每个人只能属于一个帮派,每条消息包含两个整数A,B 指A和B不在一个帮派,问消息是否矛盾
思路:
每条消息中的A,B用线连,每个人用一个Vector数组存可达的人的编号,用二分图染色法配合深搜查是否有人在两个帮派
注意:
1、输出的格式中每两组数据间需要空一行 最后一组数据不用空行~~
2、之前标记过的点在深搜的时候可以跳过,但是在确定颜色时不能跳过~!!
代码:
#include <bits/stdc++.h> using namespace std; const int Num=550000; int color[Num],mark[Num]; vector<int> q[Num]; int dfs(int u) { mark[u]=0; if(q[u].empty()) return 1; for(int i=0;i<q[u].size();i++) { int v=q[u][i]; //这里本来加了一个if(!mark[v]) continue; 就WA了…… if(color[v]==-1) color[v]=!color[u]; else if(color[v]!=!color[u]) return 0; } for(int i=0;i<q[u].size();i++){ int v=q[u][i]; if(mark[v]&&!dfs(v)) return 0; } return 1; } int main() { // freopen("1.txt","r",stdin); int t,cas=0; cin>>t; while(t--) { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) q[i].clear(); for(int i=0;i<m;i++) { int u,v;cin>>u>>v; q[u].push_back(v); q[v].push_back(u); } memset(color,-1,sizeof(color)); memset(mark,-1,sizeof(mark)); color[1]=0; int flag=1; for(int i=1;i<=n;i++) { if(mark[i]&&!dfs(i)) {flag=0;break;} // cout<<"AA"<<mark[i]<<endl; } if(cas) cout<<endl; printf("Test case #%d:\n", ++cas); if(!flag) cout<<"Something wrong!"<<endl; else cout<<"Nothing special."<<endl; } return 0; }
相关文章推荐
- 1227: Gang Black and Gang White
- HDU 5113 Black And White(暴力dfs+减枝)
- hdu 5113 Black And White
- hdu 3911 Black And White 区间合并 线段树
- HDU 5113 Black And White (dfs神剪枝)
- Off-White x Nike Air VaporMax White and Black Launching in 2018
- HDU 3633 Black and white(插头DP)
- HDU3911 Black And White(黑白子)
- hdoj 5113 Black And White(dfs, 剪枝)
- 玲珑杯 1091 - Black and White(dp计数)
- Black and white painting
- hdu 3911——Black And White
- Simple IPTables Firewall with Whitelist and Blacklist
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White (暴力)
- Ural 2063 Black and White【有意思】
- HDOJ 3911 Black And White 线段树 区间合并 成段更新
- hdu 5113 Black And White(暴力+剪枝)
- hdu 5113: Black And White(dfs+强剪枝)
- 彩色照片转换为黑白照片(Color image converted to black and white picture)