判断二分图
2014-09-05 19:24
232 查看
就是简单的判断二分图,存下代码,不用每次都重新写
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <functional> #include <sstream> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> //#pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; #define INF 1e9 #define MAXN 21 #define maxn 205 #define mod 1000000007 #define eps 1e-7 #define pi 3.1415926535897932384626433 #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define scan(n) scanf("%d",&n) #define scan2(n,m) scanf("%d%d",&n,&m) #define scans(s) scanf("%s",s); #define ini(a) memset(a,0,sizeof(a)) #define out(n) printf("%d\n",n) ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);} using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int color[maxn]; vector<int> G[maxn]; map<string,int> mp; int cnt; int insert(string s) { if(mp.find(s) == mp.end()) mp[s] = cnt++; return mp[s]; } bool dfs(int u) { rep(i,(int)G[u].size()) { int v = G[u][i]; if(color[v] == color[u]) return false; if(!color[v]) { color[v] = 3 - color[u]; if(!dfs(v)) return false; } } return true; } bool solve() { rep(i,cnt) { if(!color[i]) { color[i] = 1; if(!dfs(i)) return false; } } return true; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int T,n; cin>>T; int cas = 1; while(T--) { cin>>n; string s,t; mp.clear(); ini(color); rep(i,2*n) G[i].clear(); cnt = 0; rep(i,n) { cin>>s>>t; int a = insert(s); int b = insert(t); G[a].push_back(b); G[b].push_back(a); } printf("Case #%d: ",cas++); if(solve()) puts("Yes"); else puts("No"); } return 0; }
相关文章推荐
- hdu2444——The Accomodation of Students(判断二分图+匈牙利算法)
- hdu3478(图的联通+二分图判断)
- LZOI-二分图判断例题
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
- 判断二分图 染色法 NYOJ 1015
- hdu 5971 Wrestling Match 判断能否构成二分图 [Problem A]2016ACM/ICPC亚洲区大连站
- 二分图判断
- 无向图dfs判断是否有环和是否二分图
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- Codeforces Round #360 (Div. 1)A:二分图判断
- HDU4751Divide Groups【判断二分图】
- 判断是否为二分图
- LA 3523 Knights of the Round Table(点双连通分量+二分图判断)
- POJ 2492 二分图判断 并查集
- HDU 2444 判断二分图加最大匹配
- poj2942点双连通奇圈-二分图判断Knights of the Round Table
- hdu 2444 The Accomodation of Students (判断二分图+最大匹配)
- hdu2444 The Accomodation of Students【二分图判断+最大匹配】
- HDU 2444.The Accomodation of Students【二分图判断+最大匹配】【5月4】
- 二分图判断hdu - 1829 - A Bug's Life