标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7567 | Accepted: 2951 |
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
题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾
[l,r]中1个个数可以表示为sum[r]-sum[l-1],而题目只要求是奇偶,也就确定了sum[r]与sum[l-1]奇偶是否相同,到了这步就简单了,转变为经典模型
范围有点大,按l-1,r离散化一下
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <cmath> #include <set> #include <queue> #include <algorithm> #include <vector> #include <stack> #include <map> using namespace std; #define esp 1e-8 const double PI = acos(-1.0); const long long inf = 1000000000; const long long mod = 10000007; typedef long long LL; #pragma comment(linker, "/STACK:1024000000,1024000000") //freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取 //freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 int f[30005];int v[30005]; int a[30005], b[30005]; char s[30005][10]; set<int>st; set<int>::iterator it; map<int, int>mp; int find(int x) { if (x != f[x]) { int u = f[x]; f[x] = find(f[x]); v[x] = (v[u] + v[x]) % 2; } return f[x]; } int main() { int n, m, i, j; while (~scanf("%d%d", &n, &m)) { int ans = 0; int res = 0; st.clear(); mp.clear(); for (i = 1; i <= m; ++i) { scanf("%d%d%s", &a[i], &b[i], s[i]); st.insert(a[i]); st.insert(b[i]); } int nn = 0; for (it = st.begin(); it != st.end(); ++it) { mp[*it] = ++nn; } for (i = 0; i <= nn; ++i) f[i] = i; memset(v, 0, sizeof(v)); v[0] = 0; for (i = 1; i <= m; ++i) { int fa = find(mp[a[i]] - 1); int fb = find(mp[b[i]]); if (fa == fb)//如果相同,则已经知道了mp[a[i]] - 1和mp[b[i]]的奇偶,所以直接判断即可 { if (v[mp[a[i]] - 1] == v[mp[b[i]]] && s[i][0] == 'o') break; if (v[mp[a[i]] - 1] != v[mp[b[i]]] && s[i][0] == 'e') break; ans++; } else { f[fa] = fb; if (s[i][0] == 'o') //如果是奇数,需要加上1 { v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]] + 1) % 2; } else { v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]]) % 2; } ans++; } } printf("%d\n", ans); } }
poj 1733Parity game(map离散+带权并查集)
标签:
原文地址:http://blog.csdn.net/zjw6463/article/details/51364649