题号:1525
参见题解:作者: 超级范范 更新时间: 2017-07-23 17:05
https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P1525
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 
 | #include<iostream>#include<algorithm>
 #include<cstring>
 using namespace std;
 int father[40005];
 struct s{
 int u, v, w;
 bool operator < (s & s1){
 return w>s1.w;
 }
 }arr[100005];
 int _find(int node){
 if (father[node] == -1)return node;
 return father[node] = _find(father[node]);
 }
 void merge(int x, int y){
 x = _find(x);
 y = _find(y);
 if (x != y)father[x] = y;
 }
 bool sameset(int x, int y){
 return _find(x) == _find(y);
 }
 int main(){
 memset(father, -1, sizeof(father));
 ios::sync_with_stdio(false);
 int n, m;
 cin >> n >> m;
 for (int i = 1; i <= m; i++){
 cin >> arr[i].u >> arr[i].v >> arr[i].w;
 }
 sort(arr + 1, arr + m + 1);
 int maxw = 0;
 for (int i = 1; i <= m; i++){
 if (sameset(arr[i].u, arr[i].v)){
 if (arr[i].w > maxw){
 cout << arr[i].w;
 return 0;
 }
 }
 else{
 merge(arr[i].u, arr[i].v + n);
 merge(arr[i].v, arr[i].u + n);
 }
 }
 cout << 0;
 }
 | 
20180814更新:除了并查集做法还有二分图做法,应该也很好做,不写了。
题解:https://www.luogu.org/blog/Kesdiael3/solution-p1525
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | #include<iostream>#include<algorithm>
 using namespace std;
 int n,m;
 struct p{
 int a,b,c;
 bool operator < (const p& p2) const{
 return c>p2.c;
 }
 }ps[100005];
 int father[40005];
 int _find(int x){
 if(father[x]==0)return x;
 return father[x]=_find(father[x]);
 }
 bool _union(int x,int y){
 if(_find(x)==_find(y))return false;
 father[_find(x)]=_find(y);
 return true;
 }
 int main(){
 ios::sync_with_stdio(false);
 cin>>n>>m;
 for(int i=1;i<=m;i++)cin>>ps[i].a>>ps[i].b>>ps[i].c;
 sort(ps+1,ps+m+1);
 for(int i=1;i<=m;i++){
 if(_find(ps[i].a)==_find(ps[i].b)){
 cout<<ps[i].c;
 return 0;
 }else{
 _union(ps[i].a,ps[i].b+n);
 _union(ps[i].b,ps[i].a+n);
 }
 }
 cout<<0;
 }
 |