2959: 长跑

传送门

又是一道卡常题..因为只有link没有cut,判断两点有没有联通另开一个并查集,用find_root会T...

lct维护,把环用并查集合并即可.

  1 //Achen
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdlib>
  6 #include<vector>
  7 #include<cstdio>
  8 #include<queue>
  9 #include<cmath>
 10 #include<set>
 11 #include<map>
 12 #define For(i,a,b) for(int i=(a);i<=(b);i++)
 13 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
 14 const int N=2e5+7;
 15 typedef int LL; 
 16 typedef double db;
 17 using namespace std;
 18 int n,m;
 19 
 20 template<typename T> void read(T &x) {
 21     char ch=getchar(); x=0; T f=1;
 22     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
 23     if(ch=='-') f=-1,ch=getchar();
 24     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
 25 }
 26 
 27 int fa[N],fa2[N];
 28 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
 29 
 30 int find2(int x) { return x==fa2[x]?x:fa2[x]=find2(fa2[x]); }
 31 
 32 int p[N],ch[N][2],flip[N];
 33 LL sz[N],tsz[N],sum[N];
 34 #define lc ch[x][0]
 35 #define rc ch[x][1]
 36 void update(int x) { sum[x]=sum[lc]+sum[rc]+sz[x]; }
 37 
 38 void down(int x) {
 39     if(!flip[x]) return;
 40     swap(lc,rc);
 41     flip[lc]^=1;
 42     flip[rc]^=1;
 43     flip[x]^=1;
 44 }
 45 
 46 int isroot(int x) { p[x]=find(p[x]); return ch[p[x]][0]!=x&&ch[p[x]][1]!=x; }
 47 
 48 void rotate(int x) {
 49     int y=find(p[x]),z=find(p[y]),l=(x==ch[y][1]),r=l^1;
 50     if(!isroot(y)) ch[z][y==ch[z][1]]=x; p[x]=z;
 51     ch[y][l]=ch[x][r]; p[ch[x][r]]=y;
 52     ch[x][r]=y; p[y]=x;
 53     update(y); update(x);
 54 }
 55 
 56 void splay(int x) {
 57     static int g[N],top=0,tp;
 58     for(tp=x;!isroot(tp);tp=find(p[tp])) g[++top]=tp;
 59     g[++top]=tp;
 60     while(top) down(g[top--]);
 61     for(;!isroot(x);rotate(x)) {
 62         int y=find(p[x]),z=find(p[y]);
 63         if(!isroot(y)) ((x==ch[y][1])^(y==ch[z][1]))?rotate(x):rotate(y);
 64     }
 65 }
 66 
 67 void access(int x) {
 68     for(int t=0;x;x=find(p[t=x])) {
 69         splay(x);
 70         rc=t;
 71         update(x);
 72     }
 73 }
 74 
 75 void newroot(int x) {
 76     access(x);
 77     splay(x);
 78     flip[x]^=1;
 79 }
 80 
 81 void lik(int x,int y) {
 82     newroot(x);
 83     p[x]=y;
 84 }
 85 
 86 void change(int x,int y) {
 87     splay(x);
 88     sz[x]+=y;
 89     update(x);
 90 }
 91 
 92 void qry(int x,int y) {
 93     if(find2(x)!=find2(y)) {
 94         puts("-1"); return;
 95     }
 96     newroot(x);
 97     access(y);
 98     splay(y);
 99     printf("%d\n",sum[y]);
100 }
101 
102 LL tpsum=0;
103 void dfs(int x,int FA) {
104     tpsum+=sz[x];
105     fa[x]=FA;
106     if(lc) dfs(lc,FA);
107     if(rc) dfs(rc,FA);    
108 }
109 
110 void link_it(int x,int y) {
111     int u=find(x),v=find(y);
112     if(u==v) return;
113     if(find2(u)==find2(v)) {
114         newroot(u);
115         access(v);
116         splay(v);
117         tpsum=0;
118         dfs(v,v);
119         sz[v]=sum[v]=tpsum; 
120         ch[v][0]=ch[v][1]=0;
121     }
122     else { lik(u,v); fa2[find2(u)]=find2(v); }
123 }
124 
125 //#define DEBUG
126 int main() {
127 #ifdef DEBUG
128     freopen("1.in","r",stdin);
129     //freopen(".out","w",stdout);
130 #endif
131     read(n); read(m);
132     For(i,1,n) read(sz[i]),tsz[i]=sz[i],fa[i]=fa2[i]=i;
133     For(ti,1,m) {
134         int o,x,y;
135         read(o); read(x); read(y);
136         if(o==1) link_it(x,y);
137         else if(o==2) change(find(x),y-tsz[x]),tsz[x]=y;
138         else qry(find(x),find(y));
139     }
140     return 0;
141 }
View Code

 

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<ctime>
#include<set>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long LL; 
typedef double db;
int vis[1000007],fa[100007],cnt[100008][11],ok[5007][5007];

using namespace std;  

int find(int x) { return x==fa[x]?x:find(fa[x]); }

//#define DEBUG
int main() {
#ifdef DEBUG
	//freopen("line.in","r",stdin);
	freopen("std.in","w",stdout);
#endif
    srand(time(0)); 
    int n=500,m=1000,c=10,q=500;
    printf("%d %d %d %d\n",n,m,c,q);
    For(i,1,n*c) fa[i]=i;
    For(i,1,n) {
    	int x=rand()%10000+1;
    	printf("%d\n",x);
    }
    For(i,1,m) {
    	int u,v,w;
    	u=rand()%n+1; v=rand()%n+1; w=rand()%c;
    	while(ok[u][v]||cnt[u][w]+1>2||cnt[v][2]+1>2||(find(w*n+u)==find(w*n+v))) {
    		u=rand()%n+1;
    		v=rand()%n+1;
    		w=rand()%c;
    	}
    	ok[u][v]=1;
    	fa[find(w*n+u)]=find(w*n+v);
    	cnt[u][w]++; cnt[v][w]++;
    	printf("%d %d %d\n",u,v,w);
    }
    For(i,1,q) {
    	int o=rand()%3;
    	if(o==0) {
    		int x=rand()%n+1,y=rand()%10000+1;
    		printf("%d %d %d\n",o,x,y);
    	}
    	else if(o==1) {
    		int u=rand()%n+1,v=rand()%n+1,w=rand()%c;
    		printf("%d %d %d %d\n",o,u,v,w);
    	}
    	else {
    		int w=rand()%c,u=rand()%n+1,v=rand()%n+1;
    		while(u==v) { u=rand()%n+1; v=rand()%n+1; }
    		printf("%d %d %d %d\n",o,w,u,v);
    	}
    }
    return 0;  
}  

 

转载于:https://www.cnblogs.com/Achenchen/p/8981457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值