标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7288 | Accepted: 2833 |
Description
Input
Output
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd
Sample Output
3
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int MAX = 5010; 7 int a[2*MAX],father[2*MAX],r[2*MAX]; 8 struct Node 9 { 10 int u,v; 11 char str[10]; 12 }; 13 Node que[MAX]; 14 int n,m,cnt; 15 int Bin(int x) 16 { 17 int r = cnt - 1; 18 int l = 0; 19 20 while(r >= l) 21 { 22 int mid = (r + l) / 2; 23 if(a[mid] > x) 24 r = mid - 1; 25 else if(a[mid] < x) 26 l = mid + 1; 27 else 28 return mid; 29 } 30 return -1; 31 } 32 int find_father(int x) 33 { 34 if(father[x] == x) 35 return x; 36 int t = find_father(father[x]); 37 r[x] = r[x] ^ r[father[x]]; 38 return father[x] = t; 39 } 40 int main() 41 { 42 while(scanf("%d%d",&n,&m) != EOF) 43 { 44 cnt = 0; 45 for(int i = 1; i <= m; i++) 46 { 47 scanf("%d%d%s", &que[i].u,&que[i].v,que[i].str); 48 que[i].u --; 49 a[cnt++] = que[i].u; 50 a[cnt++] = que[i].v; 51 } 52 sort(a,a + cnt); 53 cnt = unique(a,a + cnt) - a; 54 for(int i = 1; i <= cnt; i++) 55 { 56 father[i] = i; 57 r[i] = 0; 58 } 59 int ans = 0; 60 for(int i = 1; i <= m; i++) 61 { 62 int x = Bin(que[i].u); 63 int y = Bin(que[i].v); 64 int fx = find_father(x); 65 int fy = find_father(y); 66 if(fx == fy) 67 { 68 if(r[x] == r[y] && strcmp(que[i].str,"odd") == 0) 69 break; 70 if(r[x] != r[y] && strcmp(que[i].str,"even") == 0) 71 break; 72 ans++; 73 } 74 else 75 { 76 if(strcmp(que[i].str,"odd") == 0) 77 { 78 father[fx] = fy; 79 r[fx] = r[x]^ 1 ^ r[y]; 80 } 81 else 82 { 83 father[fy] = fx; 84 r[fy] = r[x] ^ r[y] ^ 0; 85 } 86 ans ++; 87 } 88 } 89 printf("%d\n",ans); 90 } 91 92 93 94 return 0; 95 }
标签:
原文地址:http://www.cnblogs.com/zhaopAC/p/5018029.html