标签:
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #include <utility> 7 #include <iomanip> 8 #include <string> 9 #include <cmath> 10 #include <queue> 11 #include <map> 12 13 const int MAXN = 5000 + 10; 14 const int MAX = 32000 + 10; 15 using namespace std; 16 struct DATA{ 17 int num, order; 18 bool operator < (DATA b)const{ 19 return num < b.num; 20 } 21 }data[MAXN * 2];//data记录的是出现过的数字 22 struct DATA2{ 23 int a, b, type;//表示sum[b] - sum[a - 1]的是否是同一类型 24 }data2[MAXN];//直接的输入数据 25 int parent[MAXN * 2]; 26 int val[MAXN * 2]; 27 28 int n, q, cnt; 29 30 void init(){ 31 scanf("%d%d", &n, &q); 32 for (int i = 1; i <= q; i++){ 33 char str[10]; 34 scanf("%d%d", &data2[i].a, &data2[i].b); 35 data[i * 2 - 1].num = data2[i].a; data[i * 2 - 1].order = i * 2 - 1; 36 data[i * 2].num = data2[i].b; data[i * 2].order = i * 2; 37 scanf("%s", str); 38 //odd是奇数,even是偶数 39 if (str[0] == ‘o‘) data2[i].type = 0; 40 else if (str[0] == ‘e‘) data2[i].type = 1; 41 } 42 //准备离散化 43 sort(data + 1, data + 1 + 2 * q); 44 cnt = 0;//记录数字的 45 data[0].num = -100; 46 for (int i = 1; i <= 2 * q; i++){ 47 if (data[i].num != data[i - 1].num) ++cnt; 48 if (data[i].order % 2 == 1) data2[((data[i].order - 1) / 2) + 1].a = cnt; 49 else data2[((data[i].order - 1) / 2) + 1].b = cnt; 50 } 51 /*for (int i = 1;i <= q; i++){ 52 printf("%d %d %d\n", data2[i].a, data2[i].b, data2[i].type); 53 }*/ 54 } 55 int find(int x){ 56 int tmp = 0, f = x; 57 //注意要加上路径压缩 58 while (x != parent[x]){ 59 tmp += val[x]; 60 x = parent[x]; 61 } 62 parent[f] = x; 63 val[f] = tmp % 2; 64 return x; 65 } 66 67 void work(){ 68 //val[i] = 1则与父亲的奇偶性不同,0则相同 69 for (int i = 1; i <= cnt; i++){ 70 parent[i] = i; 71 val[i] = 0; 72 } 73 for (int i = 1; i <= q; i++){ 74 int x = data2[i].a, y = data2[i].b ,t = data2[i].type; 75 y++; 76 if (i == 2) 77 printf(""); 78 int xx = find(x), yy = find(y), flag = 0; 79 if (xx == yy){//同根 80 if (t == 1){//需要相同 81 if (val[x] != val[y]) flag = 1; 82 //相同则躲过一劫 83 }else{//需要不同 84 if (val[x] == val[y]) flag = 1; 85 } 86 }else{//不管怎么样都要合并 87 if (t == 1){ 88 parent[xx] = y; 89 val[xx] = (-val[x] + 2) % 2; 90 }else{ 91 parent[xx] = y; 92 val[xx] = (-val[x] + 3) % 2; 93 } 94 } 95 if (flag == 1) {printf("%d\n", i - 1);return;} 96 } 97 printf("%d\n", q); 98 } 99 100 int main(){ 101 int T; 102 #ifdef LOCAL 103 freopen("data.txt", "r", stdin); 104 freopen("out.txt", "w", stdout); 105 #endif 106 init(); 107 work(); 108 return 0; 109 }
。
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4321802.html