本文主要是介绍bnuoj 17184 代数 POJ 1733 (带权并查集 ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
N. 代数
现有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 5 1 3 3 1 3 2 2 3 2 1 1 0 1 1 1
Sample Output
Accepted! Error! Accepted! Error! Accepted! 1 Unknown! 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;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6637 | Accepted: 2580 |
Description
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
Output
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 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;
}
这篇关于bnuoj 17184 代数 POJ 1733 (带权并查集 )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!