5 条题解
-
-2
#include <bits/stdc++.h> using namespace std;
struct node { long long u, v, w; }a[500020]; long long fa[500020], tot, ans;
bool cmp(node x, node y) { return x.w < y.w; } long long get(int x) { if (fa[x] == x) return x; return fa[x] = get(fa[x]); }
int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, cnt = 0; cin >> n >> m; for (int i = 1; i <= n; i++) { fa[i] = i; } for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; a[++cnt].u= u; a[cnt].v = v; a[cnt].w = w; } sort(a + 1, a + cnt + 1, cmp); for (int i = 1; i <= m; i++) { long long u = a[i].u; long long v = a[i].v; long long w = a[i].w; if (get(u) != get(v)) { if(ans<w)ans=w; tot++; fa[get(u)] = get(v); if (tot == n - 1) { break; }
} } cout << ans << endl; return 0;
}
-
-2
#include<bits/stdc++.h> using namespace std; #define int long long struct node { int u,v,w; }; node a[1000001]; int fa[1000001]; int get(int x) { if(fa[x]==x)return x; return fa[x]=get(fa[x]); } bool cmp(node x,node y) { return x.w<y.w; } main() { ios::sync_with_stdio(0); cout.tie(0);cin.tie(0); int n,m,ans=0,tot=0; cin>>n>>m; for(int i=1;i<=m;i++) { cin>>a[i].u>>a[i].v>>a[i].w; } for(int i=1;i<=n;i++)fa[i]=i; sort(a+1,a+1+m,cmp); for(int i=1;i<=m;i++) { int u=a[i].u; int v=a[i].v; int w=a[i].w; if(get(u)!=get(v)) { ans=max(ans,w); fa[get(u)]=get(v); tot++; if(tot==n-1)break; } } cout<<ans; return 0; }
-
-6
AC
#include <bits/stdc++.h> #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define re register #define int long long using namespace std; typedef long long ll; struct node { int strt; int to; int len; }; bool cmp(node x, node y) { return x.len<y.len; } int n, m, x, y, z, ans; vector<node> edges; int parent[500010]; int fnd(int t) { return (parent[t]==t?t:parent[t]=fnd(parent[t])); } signed main() { IOS; cin>>n>>m; for(int i=1; i<=n; i++) parent[i]=i; for(int i=0; i<m; i++) cin>>x>>y>>z, edges.push_back({x, y, z}); sort(edges.begin(), edges.end(), cmp); for(int i=0; i<m; i++) { if(fnd(edges[i].strt)!=fnd(edges[i].to)) { parent[fnd(edges[i].strt)]=fnd(edges[i].to); ans=max(ans, edges[i].len); } } cout<<ans; return 0; }
- 1
信息
- ID
- 474
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 3
- 标签
- 递交数
- 148
- 已通过
- 75
- 上传者