step2
求最右
支持两种操作:
- 删除节点操作
- 询问某一节点右侧没有被删除的第一个节点。
//直线
const int N = 1e6 + 10;
int f[N];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) f[i] = i;
f[n + 1] = n + 1;
for(int i = 1; i <= m; i++)
{
char opt;
int x;
scanf("\n%c%d", &opt, &x);
if(opt == '-')
if(f[x] == x)
f[x] = x + 1;
else
{
int y = findf(x);
if(y == n + 1) printf("-1\n");
else printf("%d\n", y);
}
}
return 0;
}
//环形
int f[N];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
void dsu(int x, int y)
{
int fx = findf(x);
int fy = findf(y);
if(fx!= fy) f[fx] = fy;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) f[i] = i;
f[n + 1] = 1;
for(int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
int y = findf(x);
printf("%d ",y);
dsu(y, y + 1);
}
return 0;
}
区间合并
支持三种操作:
- 合并两点
- 合并一段区间
- 询问两点是否在同一集合种
const int N = 2e5 + 10;
int f[N], nxt[N];
int findf(int x)
{
if(x == f[x]) return x;
f[x] = findf(f[x]);
return f[x];
}
int join(int x, int y)
{
int fx = findf(x);
int fy = findf(y);
if(fx != fy) f[fy] = fx;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
f[i] = i, nxt[i] = i + 1;
for(int i = 1; i <= m; i++)
{
int opt, x, y;
scanf("%d%d%d", &opt, &x, &y);
if(opt == 1) join(x, y);
if(opt == 2)
{
int tar;
for(int i = x + 1; i <= y; i = tar)
{
join(x, i);
tar = nxt[i];
nxt[i] = nxt[y];
}
}
if(opt == 3)
{
int fx = findf(x);
int fy = findf(y);
if(fx == fy) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
带权并查集
支持两种操作:
- boss A 变成boss B的下级
- 询问x有多少层上级
int f[N], r[N];
int findf(int x)
{
if(x == f[x]) return f[x];
int p = f[x];
f[x] = findf(f[x]);
r[x] += r[p];
return f[x];
}
int join(int x, int y)
{
f[x] = y;
r[x] = 1;
}
int main()
{
int n, m, opt, x, y;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
f[i] = i, r[i] = 0;
for(int i = 1; i <= m; i++)
{
scanf("%d", &opt);
if(opt == 1)
{
scanf("%d%d", &x, &y);
join(x, y);
}
else
{
scanf("%d", &x);
int fx = findf(x);
printf("%d\n", r[x]);
}
}
return 0;
}
最小生成树
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const int M = 2e5 + 10;
const int inf = 2e9 + 1;
struct EDGE{
int x, y, v;
bool operator < (const EDGE & other)
const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
int main()
{
int n, m;
LL ans = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
for(int i = 1; i <= n; i++) f[i] = i;
sort(E + 1, E + m + 1);
int cnt = n;
for(int i = 1; i <= m; i++)
{
int fx = findf(E[i].x);
int fy = findf(E[i].y);
if(fx != fy)
{
f[fx] = fy;
cnt--;
ans += E[i].v;
}
if(cnt == 1) break;
}
cout<<ans<<endl;
return 0;
}
边权差最小的生成树
边权最大值与最小值的差最小
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const int M = 1e4 + 10;
const int inf = 2e9 + 1;
struct EDGE{
int x, y, v;
bool operator < (const EDGE & other)
const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
int main()
{
int n, m;
int ans = inf;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
sort(E + 1, E + m + 1);
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++) f[j] = j;
int cnt = n;
for(int j = i; j <= m; j++)
{
int fx = findf(E[j].x);
int fy = findf(E[j].y);
if(fx != fy)
{
f[fx] = fy;
cnt--;
}
if(cnt == 1)
{
ans = min(ans, E[j].v - E[i].v);
break;
}
}
}
if(ans < inf) printf("YES\n%d\n",ans);
else printf("NO\n");
return 0;
}
最大值最小生成树
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int inf = 2e9 + 1;
struct EDGE{
int x, y, v;
bool operator < (const EDGE & other)
const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
int main()
{
int n, m;
int ans = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
for(int i = 1; i <= n; i++) f[i] = i;
sort(E + 1, E + m + 1);
int cnt = n;
for(int i = 1; i <= m; i++)
{
int fx = findf(E[i].x);
int fy = findf(E[i].y);
if(fx != fy)
{
f[fx] = fy;
cnt--;
}
if(cnt == 1)
{
ans = E[i].v;
break;
}
}
cout<<ans<<endl;
return 0;
}
删除数量尽可能多的边,使得图依旧联通且删去的边权和不大于s
构造最大生成树,再从小到大加入边直到剩余的边的边权和小于等于s。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 5e4 + 10;
const int M = 1e5 + 10;
vector <int> vec;
struct EDGE{
int x, y, v, id;
bool operator <(const EDGE & other)
const {return v > other.v || (v == other.v && id > other.id); }
}E[M];
int f[N], vist[M];
int findf(int x)
{
if(f[x] == x) return x;
f[x] = findf(f[x]);
return f[x];
}
int main()
{
int n, m;
LL s, sum = 0, now = 0;
scanf("%d%d", &n, &m);
cin>>s;
for(int i = 1; i <= n; i++) f[i] = i;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
E[i].id = i;
sum = sum + E[i].v;
}
sort(E + 1, E + m + 1);
int cnt = n;
for(int i = 1; i <= m; i++)
{
if(cnt == 1) break;
int fx = findf(E[i].x);
int fy = findf(E[i].y);
if(fx != fy)
{
f[fx] = fy;
cnt--;
vist[i] = 1;
now += E[i].v;
}
}
for(int i = 1; i <= m; i++)
{
if(now >= sum - s) break;
if(!vist[i])
{
vist[i] = 1;
now += E[i].v;
}
}
for(int i = 1; i<= m; i++)
if(!vist[i])
vec.push_back(E[i].id);
printf("%d\n", vec.size());
sort(vec.begin(), vec.end());
for(int i = 0; i < vec.size(); i++)
printf("%d ",vec[i]);
printf("\n");
return 0;
}
黑白染色(带权并查集)
一条边连接的两个点颜色不同,判断两点是否颜色相同。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 2e5 + 10;
int f[N], r[N];
int findf(int x)
{
if(f[x] == x) return x;
int p = f[x];
f[x] = findf(f[x]);
r[x] = (r[p] + r[x]) % 2;
return f[x];
}
void join(int x, int y)
{
int fx = findf(x);
int fy = findf(y);
if(fx != fy)
{
f[fx] = fy;
r[fx] = (r[x] + r[y] + 1) % 2;
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
f[i] = i;
int shift = 0;
for(int i = 1; i <= m; i++)
{
int opt, x, y;
scanf("%d%d%d", &opt, &x, &y);
x = (x + shift) % n;
y = (y + shift) % n;
x++; y++;
if(opt == 0)
join(x, y);
else
{
int fx = findf(x);
int fy = findf(y);
if(r[x] == r[y])
{
shift = (shift + 1) % n;
printf("YES\n");
}
else
printf("NO\n");
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 3e5 + 10;
int f[N], r[N];
int findf(int x)
{
if(f[x] == x) return x;
int p = f[x];
f[x] = findf(f[x]);
r[x] = (r[p] + r[x]) % 2;
return f[x];
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) f[i] = i;
for(int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
int fx = findf(x);
int fy = findf(y);
if(fx != fy)
{
f[fx] = fy;
r[fx] = (r[x] + r[y] + 1) % 2;
}
else
{
if(r[x] == r[y])
{
printf("%d\n", i);
return 0;;
}
}
}
printf("-1\n");
return 0;
}