标签:problem contain 两种 har == otherwise pos define 代码
题目链接:http://poj.org/problem?id=2513
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions:40949 | Accepted: 10611 |
1 #include<stdio.h> 2 #include<string.h> 3 #define mem(a, b) memset(a, b, sizeof(a)) 4 const int MAXN = 250000 * 2 + 10;//最多MAXN种颜色 即最多MAXN种不同的点 5 6 char s1[12], s2[12]; 7 int deg[MAXN]; 8 int trie[MAXN][27], cnt, tot; 9 int id[MAXN], pre[MAXN]; 10 11 int insert(char s[12]) 12 { 13 int flag = 1; 14 int len = strlen(s); 15 int root = 0; 16 for(int i = 0; i < len; i ++) 17 { 18 int id = s[i] - ‘a‘; 19 if(!trie[root][id]) 20 { 21 flag = 0; //单词之前没出现过 22 trie[root][id] = ++ cnt; 23 } 24 root = trie[root][id]; 25 } 26 if(!flag) 27 { 28 tot ++; 29 id[root] = tot; 30 return id[root]; 31 } 32 else 33 return id[root]; 34 } 35 36 int find(int x) 37 { 38 if(pre[x] == x) 39 return x; 40 else 41 { 42 int root = find(pre[x]); 43 pre[x] = root; 44 return pre[x]; 45 } 46 } 47 48 int main() 49 { 50 for(int i = 1; i <= MAXN + 1; i ++) 51 pre[i] = i; 52 while(scanf("%s%s", s1, s2) != EOF) 53 { 54 int a = insert(s1), b = insert(s2);//返回的是颜色所对应的序号 55 deg[a] ++, deg[b] ++; //点的度数 判断是否存在欧拉路径 56 int x = find(a), y = find(b); //查找根节点 57 if(x != y) 58 pre[y] = x; 59 } 60 int flag = 1; 61 for(int i = 1; i < tot; i ++) //总共有tot个不同的点 62 if(find(i) != find(i + 1)) 63 { 64 flag = 0; 65 break; 66 } 67 if(flag == 0) 68 printf("Impossible\n"); 69 else 70 { 71 int xx = 0; 72 for(int i = 1; i <= tot; i ++) 73 if(deg[i] % 2) 74 xx ++; 75 if(xx == 0 || xx == 2) 76 printf("Possible\n"); 77 else 78 printf("Impossible\n"); 79 } 80 return 0; 81 }
标签:problem contain 两种 har == otherwise pos define 代码
原文地址:https://www.cnblogs.com/yuanweidao/p/11296325.html