
并查集
幽殇默
他时若遂凌云志,敢笑黄巢不丈夫。
展开
-
B1. Books Exchange (easy version)【1000 / 并查集】
https://codeforces.com/problemset/problem/1249/B1 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],p[N],cnt[N],n,t; int find(int x) { if(x!=p[x]) p[x]=find(p[x]); return p[x]; } int main(void) { cin>>t; while(t--).原创 2021-10-16 15:15:44 · 154 阅读 · 0 评论 -
238. 银河英雄传说【边带权的并查集】
https://www.acwing.com/problem/content/description/240/ d[x]表示的是x到其根节点的距离 小呆呆题解 #include<bits/stdc++.h> using namespace std; const int N=1e4*3+10; int p[N],d[N],cnt[N],t; int find(int x) { if(x!=p[x]) { int root=find(p[x]); d.原创 2021-10-11 20:17:59 · 141 阅读 · 2 评论 -
237. 程序自动分析【并查集】
https://www.acwing.com/problem/content/description/239/ 这里的话,需要注意的是我们的范围特别的大,如果直接开数组会MLE。 所以这里我是直接用了哈希表来映射。然后我们保存状态,先处理全部的相等的条件,再处理所有的不等条件。 然后判断有没有矛盾点。 当然这里的方法有点极限卡时间。 #include<bits/stdc++.h> using namespace std; int t,n; struct node {int a,b,c;}tem.原创 2021-10-08 11:42:59 · 129 阅读 · 0 评论 -
1252. 搭配购买【并查集 + 01背包】
https://www.acwing.com/problem/content/1254/ 先处理并查集,将每一个连通块当成一个物品,这就转化成了01背包问题。 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; int n,m,w; int p[N],price[N],s[N];//并查集 int V[N],W[N],f[N];//01背包 int find(int x) { if(x!=p[x]) p[x].原创 2021-10-07 09:22:15 · 121 阅读 · 0 评论 -
1250. 格子游戏【并查集】
https://www.acwing.com/problem/content/1252/ 将二维坐标,转换成1维坐标,然后有环退出。 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; int n,m,p[N]; struct node { int x,y; char op; }temp; vector<node>ve; int find(int x) { if(x!=p[x]) .原创 2021-10-06 10:51:04 · 393 阅读 · 0 评论 -
小希的迷宫【并查集判环】
http://acm.hdu.edu.cn/showproblem.php?pid=1272 #include<bits/stdc++.h> using namespace std; int n,m,p[100005]; vector<pair<int,int>>ve; int find(int x) { if(x!=p[x]) p[x]=find(p[x]); return p[x]; } int main(void) { while(cin>>n&.原创 2021-09-27 16:51:56 · 149 阅读 · 0 评论 -
149. 这是一棵树吗【并查集】
https://www.papamelon.com/problem/149 #include<bits/stdc++.h> using namespace std; const int N=1e5*2+10; int n,m,p[N]; int find(int x) { if(x!=p[x]) p[x]=find(p[x]); return p[x]; } int main(void) { while(cin>>n>>m) { for(int i=1;.原创 2021-09-27 15:49:01 · 93 阅读 · 0 评论 -
【1213】 How Many Tables 【并查集 / 简单】
http://acm.hdu.edu.cn/showproblem.php?pid=1213 经典的并查集题: #include<cstdio> #include<iostream> using namespace std; const int N=1e3+5; int p[N]; int find(int x) { if(x!=p[x]) p[x]=find(p[x]); return p[x]; } int main(void) { int t; cin>>t.原创 2021-05-08 10:30:37 · 114 阅读 · 0 评论