二分图判断
来源:互联网 发布:易语言网络爬虫 编辑:程序博客网 时间:2024/06/05 12:48
https://gcj-prod.appspot.com/codejam/contest/2933486/dashboard
bfs看是否有染色矛盾,两个都没染过色 说明这是另一个图,再次bfs
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <string>#include <map>using namespace std;int ca,n;const int maxn = 11000;map <string,int> mp;string a[maxn],b[maxn];bool vid[maxn*2];int tot,head[maxn*2],col[maxn*2];struct node{ int v,next;}edges[maxn*2];void add(int u,int v){ edges[tot].v = v;edges[tot].next = head[u];head[u] = tot++; edges[tot].v = u;edges[tot].next = head[v];head[v] = tot++;}void init(){ scanf("%d",&n); for(int i = 0 ; i <= 2*n+1 ; i++) head[i] = -1,col[i] = 0; mp.clear(); for(int i = 1 ; i <= n ; i++){ cin >> a[i]>>b[i]; if(mp[a[i]] == 0) mp[a[i]] = i*2; if(mp[b[i]] == 0) mp[b[i]] = i*2+1; add(mp[a[i]],mp[b[i]]); }}int bfs(int ka,int kb){ queue<int>q; q.push(ka); col[ka] = 1; while(!q.empty()){ int tmp = q.front();q.pop(); // cout <<"asd "<<endl; for(int k = head[tmp] ; k != -1; k = edges[k].next){ int v = edges[k].v; //cout<<"v = "<<v << " col[v] = "<<col[v]<<" tmp = "<<tmp <<" col[tmp = "<<col[tmp]<<endl; if(col[v]!=0 && col[v] == col[tmp]) return -1; else if(col[v] == 0){ q.push(v); // cout << "V = "<<v<<endl; col[v] = col[tmp] == 1? 2:1; } } } return 1;}void sov(){ for(int i = 1; i <= n ; i++){ int ka = mp[a[i]],kb = mp[b[i]]; if(col[ka] == 0 && col[kb] == 0){ if(bfs(ka,kb) == -1){ printf("Case #%d: No\n",ca); return; } } } printf("Case #%d: Yes\n",ca);}int main(){ int T; freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); scanf("%d",&T); for(ca = 1; ca <= T ;ca++){ init(); sov(); }}
0 0
- 二分图判断
- 判断二分图
- 二分图判断
- 二分图的判断
- 判断二分图 bfs
- 二分图判断
- 二分图判断
- HDU4751 二分图判断
- 二分图判断
- 二分图的判断
- hdu3478 Catch (二分图判断)
- 判断是否为二分图
- hdu 1829 (判断二分图)
- hdu4751(判断二分图)
- poj--1703 二分图判断
- hdu4751 二分图的判断
- nyoj 1015 二分图判断
- hdu4751Divide Groups 二分图判断
- 腾讯小白作业4.16.html
- 二叉树的最大深度
- C++ vector
- android DialogFragment添加动画
- 投掷硬币
- 二分图判断
- Spark2.X 原码分析 ---- Rpc初探
- 软链接/硬链接/fd
- 做产品的思路
- 用bash升级python第三方库
- 线程的互斥与同步通信-笔记整理3
- 07、React系列之--使用jspm管理
- Android开发艺术探索读书(三)-View的事件体系
- leetcode 409. Longest Palindrome