代码模板2
:::::info[图论] ::::info[并查集] :::success[并查集]
#include<bits/stdc++.h>
using namespace std;
string a,b;
string per[50010];
int bj,fa[50010],cnt;
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
}
int main()
{
cin>>a;
while(a[0]!='$'){
if(a[0]=='#'){
a.erase(0,1);
b=a;
bool v=1;
for(int i=1;i<=cnt&&v;i++)
if(per[i]==b) bj=i,v=0;
if(v){
per[++cnt]=b;
fa[cnt]=cnt;
bj=cnt;
}
}
else if(a[0]=='+'){
a.erase(0,1);
bool v=1;
int o;
for(int i=1;i<=cnt&&v;i++)
if(per[i]==a) o=i,v=0;
if(v) per[++cnt]=a,o=cnt;
fa[o]=fa[bj];
}
else{
a.erase(0,1);
cout<<a<<" ";
for(int i=1;i<=cnt;i++){
if(per[i]==a){
cout<<per[find(i)]<<"\n";
break;
}
}
}
cin>>a;
}
return 0;
}//P2814
::: :::success[最小生成树]
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,z,fa[200010],cnt,sum;
struct tree{
int x,y,z;
}a[200010];
bool cmp(tree u,tree v){
return u.z<v.z;
}
int fin(int o){
if(fa[o]==o) return o;
else return fa[o]=fin(fa[o]);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>a[i].x>>a[i].y>>a[i].z;
for(int i=1;i<=n;i++) fa[i]=i;
sort(a+1,a+m+1,cmp);
for(int i=1;cnt<=n-1&&i<=m;i++){
if(fin(a[i].x)==fin(a[i].y)) continue;
// cout<<a[i].x<<" "<<a[i].y<<" "<<a[i].z<<"\n";
cnt++;
fa[fin(a[i].x)]=fin(a[i].y);
sum+=a[i].z;
}
if(cnt!=n-1) cout<<"orz";
else cout<<sum;
return 0;
}
| ::: |
|---|
| ::::info[最短路] |
| :::success[SPFA] |
| ~~~cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int t,n,m,u,y,w,dis[2010],v[2010]; |
| bool vis[2010]; |
| struct huan{ |
| int x,len; |
| }; |
| vector<huan>a[2010]; |
| queue<int>q; |
| void clean(){ |
| queue<int>h; |
| swap(q,h); |
| for(int i=1;i<=n;i++){ |
| vector<huan>d; |
| swap(a[i],d); |
| } |
| } |
| bool spfa(){ |
| q.push(1); |
| v[1]=0; |
| while(!q.empty()){ |
| int t=q.front(); |
| q.pop(); |
| vis[t]=0; |
| dis[t]++; |
| if(dis[t]==n) return 1; |
| int e=a[t].size(); |
| for(int i=0;i<e;i++) |
| if(v[a[t][i].x]>v[t]+a[t][i].len){ |
| v[a[t][i].x]=v[t]+a[t][i].len; |
| if(!vis[a[t][i].x]) |
| q.push(a[t][i].x); |
| } |
| } |
| return 0; |
| } |
| int main() |
| { |
| cin>>t; |
| for(int i=1;i<=t;i++){ |
| cin>>n>>m; |
| for(int i=0;i<=n;i++) dis[i]=vis[i]=0,v[i]=INT_MAX; |
| clean(); |
| for(int j=1;j<=m;j++){ |
| cin>>u>>y>>w; |
| a[u].push_back((huan){y,w}); |
| if(w>=0) a[y].push_back((huan){u,w}); |
| } |
| if(spfa()) cout<<"YES\n"; |
| else cout<<"NO\n"; |
| } |
| return 0; |
| }//P3385 |
| ~~~ |
| ::: |
| :::success[Dijkstra] |
| ~~~cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int n,m,s,u,v,w,dis[100010]; |
| bool vis[100010]; |
| struct jl{ |
| int x,len; |
| bool operator<(const jl &x)const{ |
| return x.len<len; |
| } |
| }; |
| priority_queue<jl>g; |
| vector<jl>a[100010]; |
| void dijkstra(){ |
| g.push((jl){s,0}); |
| while(!g.empty()){ |
| jl t=g.top(); |
| g.pop(); |
| if(vis[t.x]) continue; |
| vis[t.x]=1; |
| int o=a[t.x].size(); |
| for(int i=0;i<o;i++) |
| if(dis[a[t.x][i].x]>a[t.x][i].len+t.len){ |
| dis[a[t.x][i].x]=a[t.x][i].len+t.len; |
| if(!vis[a[t.x][i].x]){ |
| g.push((jl){a[t.x][i].x,dis[a[t.x][i].x]}); |
| } |
| } |
| } |
| } |
| int main() |
| { |
| cin>>n>>m>>s; |
| for(int i=0;i<=n;i++) |
| if(i!=s) |
| dis[i]=INT_MAX,g.push((jl){i,dis[i]}); |
| dis[s]=0; |
| for(int i=1;i<=m;i++){ |
| cin>>u>>v>>w; |
| a[u].push_back((jl){v,w}); |
| } |
| dijkstra(); |
| for(int i=1;i<=n;i++){ |
| cout<<dis[i]<<" "; |
| } |
| return 0; |
| } |
| ~~~ |
| ::: |
| :::success[Floyd] |
| ~~~cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int n,m,dis[110][110],u,v,w; |
| int main() |
| { |
| memset(dis,0x3f,sizeof(dis)); |
| cin>>n>>m; |
| for(int i=1;i<=m;i++){ |
| cin>>u>>v>>w; |
| dis[u][v]=min(dis[u][v],w); |
| dis[v][u]=min(dis[v][u],w); |
| } |
| for(int i=1;i<=n;i++) dis[i][i]=0; |
| for(int k=1;k<=n;k++) |
| for(int x=1;x<=n;x++) |
| for(int y=1;y<=n;y++) |
| dis[x][y]=min(dis[x][y],(dis[x][k]+dis[k][y])); |
| for(int i=1;i<=n;i++,cout<<"\n") |
| for(int j=1;j<=n;j++) |
| cout<<dis[i][j]<<" "; |
| return 0; |
| }//B3647 |
| ~~~ |
| ::: |
| :::: |
| ::::info[简单树上问题] |
| :::success[树的直径] |
| ~~~cpp |
| void dfs(int u,int fa){//树形DP |
| for(auto i:a[u]){ |
| if(i.x==fa) continue; |
| dfs(i.x,u); |
| ans=max(ans,dp[u]+dp[i.x]+i.len);//更新最远距离 |
| dp[u]=max(dp[u],dp[i.x]+i.len);//更新dp[u] |
| } |
| } |
| ~~~ |
| ::: |
| :::success[树的重心] |
| ~~~cpp |
| void getroot(int u,int fa){//获取重心 |
| size[u]=1; |
| f[u]=0;//删除u后,最大子树的大小 |
| for(int i:a[u]){ |
| if(i==fa) continue; |
| getroot(i,u); |
| size[u]+=size[i]; |
| f[u]=max(f[u],size[v]); |
| } |
| f[u]=max(f[u],S-size[u]);//S为当前子树总结点数 |
| if(f[u]<f[root]) root=u; |
| }//树的重心 |
| ~~~ |
| ::: |
| :::: |
| ::::success[拓扑排序] |
| ~~~cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int in[110],n; |
| vector<int>a[110]; |
| queue<int>q; |
| int main() |
| { |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| int e; |
| while(cin>>e){ |
| if(!e) break; |
| a[i].push_back(e); |
| in[e]++; |
| } |
| } |
| for(int i=1;i<=n;i++) |
| if(!in[i]) q.push(i); |
| while(!q.empty()){ |
| int u=q.front(); |
| q.pop(); |
| for(int i=0;i<a[u].size();i++){ |
| in[a[u][i]]--; |
| if(!in[a[u][i]]) q.push(a[u][i]); |
| } |
| cout<<u<<" "; |
| } |
| return 0; |
| }//B3644 |
| ~~~ |
| :::: |
| ::::info[LCA 最近公共祖先] |
| :::success[倍增法] |
| ~~~cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int n,m,s,d[500010],p[500010][30]; |
| //d[i]表示点i的深度,p[u][i]表示u节点向上跳2^i到达的节点 |
| vector<int>a[500010]; |
| void dfs(int x,int fa){ |
| //x:当前节点,fa:x的父亲 |
| d[x]=d[fa]+1; |
| p[x][0]=fa; |
| for(int i=1;(1<<i)<=d[x];i++) |
| p[x][i]=p[p[x][i-1]][i-1];//核心 |
| int e=a[x].size(); |
| for(int i=0;i<e;i++) |
| if(a[x][i]!=fa) |
| dfs(a[x][i],x); |
| return; |
| } |
| int main() |
| { |
| cin>>n>>m>>s; |
| for(int i=1;i<n;i++){ |
| int x,y; |
| cin>>x>>y; |
| a[x].push_back(y); |
| a[y].push_back(x); |
| } |
| dfs(s,0); |
| for(int i=1;i<=m;i++){ |
| int x,y; |
| cin>>x>>y; |
| if(d[x]>d[y]) swap(x,y); |
| for(int j=20;j>=0;j--)//只能从大到小 |
| if(d[x]+(1<<j)<=d[y])//如果跳了以后不会高于d[x] |
| y=p[y][j]; |
| if(x==y){ |
| cout<<x<<"\n"; |
| continue; |
| } |
| for(int j=20;j>=0;j--) |
| if(p[x][j]!=p[y][j]){//如果不是他们的公共祖先 |
| x=p[x][j]; |
| y=p[y][j]; |
| } |
| cout<<p[x][0]<<"\n"; |
| } |
| return 0; |
| }//P3379 |
| ~~~ |
| ::: |
| :::success[Tarjan] |
| ```cpp |
| //洛谷P3379的tarjan代码 |
| #include<bits/stdc++.h> |
| using namespace std; |
| const int N=500005; |
| int fa[N],head[N],cnt,head_query[N],cnt_query,ans[N]; |
| bool vis[N]; |
| struct Edge{ |
| int to,next,num; |
| }edge[2N],query[2N];//链式前向星 |
| void init(){//链式前向星:初始化 |
| for(int i=0;i<2*N;++i){ |
| edge[i].next=-1; |
| head[i]=-1; |
| query[i].next=-1; |
| head_query[i]=-1; |
| } |
| cnt=0; |
| cnt_query=0; |
| } |
| void addedge(int u,int v){//链式前向星:加边 |
| edge[cnt].to=v; |
| edge[cnt].next=head[u]; |
| head[u]=cnt++; |
| } |
| void add_query(int x,int y,int num){//num第几个查询 |
| query[cnt_query].to=y; |
| query[cnt_query].num=num;//第几个查询 |
| query[cnt_query].next=head_query[x]; |
| head_query[x]=cnt_query++; |
| } |
| int find_set(int x){//并查集查询 |
| return fa[x]==x?x:find_set(fa[x]); |
| } |
| void tarjan(int x){//tarjan是一个DFS |
| vis[x]=true; |
| for(int i=head[x];i!=-1;i=edge[i].next){ |
| int y=edge[i].to; |
| if(!vis[y]){//遍历子结点 |
| tarjan(y); |
| fa[y]=x;//合并并查集:把子结点y合并到父结点x上 |
| } |
| } |
| for(int i=head_query[x];~i;i=query[i].next){//查询所有和x有询问关系的y |
| int y=query[i].to; |
| if(vis[y])//如果to被访问过 |
| ans[query[i].num]=find_set(y);//LCA就是find(y) |
| } |
| } |
| int main() |
| { |
| init(); |
| memset(vis,0,sizeof(vis)); |
| int n,m,root; |
| scanf("%d%d%d",&n,&m,&root); |
| for(int i=1;i<n;i++){//读n个结点 |
| fa[i]=i;//并查集初始化 |
| int u,v; |
| scanf("%d%d",&u,&v); |
| addedge(u,v); |
| addedge(v,u);//存边 |
| } |
| fa[n]=n;//并查集的结点n |
| for(int i=1;i<=m;++i){//读m个询问 |
| int a,b; |
| scanf("%d%d",&a,&b); |
| add_query(a,b,i); |
| add_query(b,a,i);//存查询 |
| } |
| tarjan(root); |
| for(int i=1;i<=m;++i) printf("%d\n",ans[i]); |
| return 0; |
| } |
| ``` |
| ::: |
| :::: |
| ::::: |
| 代码模板1,戳这里 |
| 代码模板3,戳这里 |
| 代码模板4,戳这里 |
| 用户主页,戳这里 |