bnuoj 17184 代数 && POJ 1733 (带权并查集 )

来源:互联网 发布:南京大学 软件学院 编辑:程序博客网 时间:2024/06/08 12:32

N. 代数

Case Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main

 

现有N个未知数A[1],A[2],…A[N],以及M个方程,每个方程都是形如A[s]+A[s+1]+A[s+2]+…A[t-1]+A[t]=c。现在求解这个方程组。

Input

 

输入的第一行为两个整数N和M(1<=N,M<=100000)。接下来的M行每行三个整数s,t,c(1 <= s, t <= N, 0 <= c < 10^9)。

Output

 

对于输入的每个方程,若该方程与前面的方程矛盾,则输出"Error!"并忽略这个方程,否则输出"Accepted!"。之后对于每个变量,若能求出这个变量的值,则输出这个变量,否则输出"Unknown!"。

Sample Input

3 51 3 31 3 22 3 21 1 01 1 1

Sample Output

Accepted!Error!Accepted!Error! Accepted!1Unknown!Unknown!
//      whn6325689//Mr.Phoebe//http://blog.csdn.net/u013007900#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#include <functional>#include <numeric>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long ll;typedef long double ld;typedef pair<ll, ll> pll;typedef complex<ld> point;typedef pair<int, int> pii;typedef pair<pii, int> piii;typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))#define mp(x,y) make_pair(x,y)#define pb(x) push_back(x)#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))#define eps 1e-9#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LLINF 1LL<<62template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;}template <class T>inline void write(T n){    if(n < 0)    {        putchar('-');        n = -n;    }    int len = 0,data[20];    while(n)    {        data[len++] = n%10;        n /= 10;    }    if(!len) data[len++] = 0;    while(len--) putchar(data[len]+48);}//-----------------------------------const int MAXN=100010;struct DisjointSet{int fa[MAXN];ll sum[MAXN];void init(int n){for(int i=0;i<=n;i++)fa[i]=i;CLR(sum,0);}int findfa(int x){if(fa[x]==x)return x;int xx=findfa(fa[x]);sum[x]+=sum[fa[x]];return fa[x]=xx;}bool merge(int x,int y,int num){int xx=findfa(x);int yy=findfa(y);if(xx==yy){return sum[x]-sum[y]==num;}if(yy>xx){fa[xx]=yy;sum[xx]=sum[y]+num-sum[x];}else{fa[yy]=xx;sum[yy]=sum[x]-num-sum[y];}return true;}}g;int main(){int n,m,a,b,c;scanf("%d %d",&n,&m);g.init(n);for(int i=0;i<m;i++){scanf("%d %d %d",&a,&b,&c);b++;if(g.merge(a,b,c))puts("Accepted!");elseputs("Error!");}for(int i=1;i<=n;i++){int xx=g.findfa(i),yy=g.findfa(i+1);if(xx==yy)write(g.sum[i]-g.sum[i+1]),putchar('\n');elseputs("Unknown!");}return 0;}

Parity game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6637 Accepted: 2580

Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers. 

You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either `even' or `odd' (the answer, i.e. the parity of the number of ones in the chosen subsequence, where `even' means an even number of ones and `odd' means an odd number).

Output

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

Sample Input

1051 2 even3 4 odd5 6 even1 6 even7 10 odd

Sample Output

3
//      whn6325689//Mr.Phoebe//http://blog.csdn.net/u013007900#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#include <functional>#include <numeric>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long ll;typedef long double ld;typedef pair<ll, ll> pll;typedef complex<ld> point;typedef pair<int, int> pii;typedef pair<pii, int> piii;typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))#define mp(x,y) make_pair(x,y)#define pb(x) push_back(x)#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))#define eps 1e-9#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LLINF 1LL<<62template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;}template <class T>inline void write(T n){    if(n < 0)    {        putchar('-');        n = -n;    }    int len = 0,data[20];    while(n)    {        data[len++] = n%10;        n /= 10;    }    if(!len) data[len++] = 0;    while(len--) putchar(data[len]+48);}//-----------------------------------#define ls i<<1#define rs i<<1|1const int MAXN=100010;struct DisjointSet{int fa[MAXN];int sum[MAXN];void init(int n){for(int i=0;i<=n;i++)fa[i]=i;CLR(sum,0);}int findfa(int x){if(fa[x]==x)return x;int xx=findfa(fa[x]);sum[x]+=sum[fa[x]];return fa[x]=xx;}bool merge(int x,int y,int num){int xx=findfa(x);int yy=findfa(y);if(xx!=yy){fa[xx]=yy;sum[xx]=sum[y]+num-sum[x];}else if((abs(sum[x]-sum[y])&1)!=num)return false;return true;}}g;char str[11];int a[MAXN],b[MAXN],c[MAXN];int main(){int n,m,ans=0;while(scanf("%d",&n)!=EOF&&~n){scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d %d %s",&a[ls],&a[rs],str);c[i]=str[0]=='o';a[rs]++;b[ls]=a[ls];b[rs]=a[rs];}sort(b,b+2*m);int size=unique(b,b+2*m)-b;g.init(2*m);for(int i=0;i<2*m;i++)a[i]=upper_bound(b,b+size,a[i])-b;ans=m;for(int i=0;i<m;i++){int x=a[ls];int y=a[rs];if(!g.merge(x,y,c[i])){ans=i;break;}}write(ans),putchar('\n');}return 0;}









0 0