'commit'
This commit is contained in:
58
TangDou/LuoGuBook/P1525.cpp
Normal file
58
TangDou/LuoGuBook/P1525.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 20010;
|
||||
const int M = 100010;
|
||||
|
||||
int n, m, x, y;
|
||||
|
||||
//fa[i]表示i所属的集合,用fa[i+n]表示i所属集合的补集,实现的很巧妙,可以当成一个使用并查集的巧妙应用;
|
||||
int fa[N << 1]; //并查集数组,有时也写成 N << 1 ,从左到右,读作N左移1位,就是 2*N
|
||||
|
||||
//要深入理解这个递归并压缩的过程
|
||||
int find(int x) {
|
||||
if (fa[x] != x) fa[x] = find(fa[x]);
|
||||
return fa[x];
|
||||
}
|
||||
|
||||
//加入家族集合中
|
||||
void join(int c1, int c2) {
|
||||
int f1 = find(c1), f2 = find(c2);
|
||||
if (f1 != f2)fa[f1] = f2;
|
||||
}
|
||||
|
||||
//关系
|
||||
struct node {
|
||||
int x; //第一个人
|
||||
int y; //第二个人
|
||||
int v; //冲突值
|
||||
} a[M];
|
||||
|
||||
//排序函数,按冲突值大小排序,大的在前
|
||||
bool cmp(const node &a, const node &b) {
|
||||
return a.v > b.v;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//读入
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= m; i++) cin >> a[i].x >> a[i].y >> a[i].v;
|
||||
//排序
|
||||
sort(a + 1, a + m + 1, cmp);
|
||||
|
||||
//初始化并查集,每个人都是自己的祖先
|
||||
for (int i = 1; i <= 2 * n; i++)fa[i] = i;//注意这里是2*n
|
||||
|
||||
//处理m组关系
|
||||
for (int i = 1; i <= m; i++) {
|
||||
x = find(a[i].x);
|
||||
y = find(a[i].y);
|
||||
if (x == y) {
|
||||
cout << a[i].v << endl;
|
||||
return 0;
|
||||
} else
|
||||
join(a[i].y + n, x), join(a[i].x + n, y);
|
||||
}
|
||||
cout << 0 << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user