标签:problem height 传送门 == har print int printf ret
有n个物品,m个操作,两种操作:
! a b w表示第a个物品比第b个物品重量轻w
? a b求出a比b轻多少,如果无法求出答案则输出UNKNOWN
带权并查集,对于!操作,默认编号小的在前面,如果要交换则w=-w
然后直接做就好了
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int fa[110000],v[110000]; int findfa(int x) { if(fa[x]==x) return x; int f=fa[x]; fa[x]=findfa(fa[x]); v[x]+=v[f]; return fa[x]; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(v,0,sizeof(v)); for(int i=0;i<=n;i++) fa[i]=i; bool bk=true;char st[3]; for(int i=1;i<=m;i++) { int x,y,k; scanf("%s%d%d",st+1,&x,&y); if(st[1]==‘!‘) { scanf("%d",&k); if(x>y) k=-k,swap(x,y); int fx=findfa(x),fy=findfa(y); if(fx!=fy) { fa[fy]=fx; v[fy]=k-v[y]+v[x]; } } else { int fx=findfa(x),fy=findfa(y); if(fx!=fy) printf("UNKNOWN\n"); else printf("%d\n",v[y]-v[x]); } } } return 0; }
BZOJ4690: Never Wait for Weights
标签:problem height 传送门 == har print int printf ret
原文地址:https://www.cnblogs.com/Never-mind/p/8858623.html