Codeforces Round 923 (Div. 3) F. Microcycle

题目

【坑点】:不能先用拓扑排序去掉“线头”,然后找权重最小的边所在的环。因为去掉线头后,可能有的边不在环内。

e.g.有六条无向边 1 - 2 , 2 - 3, 1 - 3, 4 - 5, 5 - 6, 4 - 6, 1 - 4,  边1 - 4不在环内

wa代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
#define ll long long
#define pii pair<int, int>
#define ld long double
const int maxn = 1e6 + 5, inf = 1e9, maxm = 4e4 + 5, base = 37;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
// const int mod = 998244353;
// const __int128 mod = 212370440130137957LL;
int n, m;

struct DSU{
	int n;
	vector<int> fa, siz;
	DSU(int n) : n(n + 1), fa(n + 1), siz(n + 1, 1){
		iota(fa.begin(), fa.end(), 0);
	}
	int find(int x){
		if(x == fa[x]) return x;
		return fa[x] = find(fa[x]);
	}
	bool merge(int x, int y){
		x = find(x), y = find(y);
		if(x == y) return 0;
		fa[x] = y;
		return 1;
	}
};
//long long ? maxn ? n? m?
void solve(){
    ll res = 0;
    int k;
    cin >> n >> m;
	// vector<int> a(n + 1);
	// for(int i = 1; i <= n; i++){
	// 	cin >> a[i];
	// }
	vector<vector<pii>> G(n + 1);
	vector<array<int, 3>> edge;
	vector<int> vis(n + 1), ind(n + 1);
	map<pii, bool> mp;
	for(int i = 1; i <= m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		G[u].pb({v, w});
		G[v].pb({u, w});
		// edge.pb({u, v, w});
		if(u > v) swap(u, v);
		if(mp.count({u, v})) continue;
		mp[{u, v}] = 1;
		ind[u]++, ind[v]++;
	}
	queue<int> q;
	for(int i = 1; i <= n; i++){
		if(ind[i] <= 1){
			q.push(i);
			vis[i] = 1;
		}
	}
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for(auto [v, w] : G[u]){
			if(--ind[v] <= 1 && !vis[v]){
				vis[v] = 1;
				q.push(v);
			}
		}
	}
	int from, to, mn = inf;
	for(int u = 1; u <= n; u++){
		if(vis[u]) continue;
		for(auto [v, w] : G[u]){
			if(vis[v]) continue;
			if(w < mn){
				mn = w;
				from = u;
				to = v;
			}
		}
	}
	bool ok = 0;
	vector<int> ans, path;
	auto dfs = [&](auto self, int u, int p) -> void {
		if(ok) return;
		if(u == to && p == from) return;
		if(vis[u]) return;
		vis[u] = 1;
		path.pb(u);
		// cout << u << '\n';
		// for(auto x : path){
		// 	cout << x << ' ';
		// }
		// cout << "\n\n";
		if(u == to){
			ans = path;
			ok = 1;
			return;
		}
		for(auto [v, w] : G[u]){
			// if(vis[v] || u == from && v == to) continue;
			// vis[v] = 1;
			if(v == p) continue;
			self(self, v, u);
		}
		path.pop_back();
	};
	// vis[from] = 1;
	dfs(dfs, from, to);
	cout << mn << ' ' << ans.size() << '\n';
	for(auto x : ans){
		cout << x << ' ';
	}
	if(ans.size() == 0){
		cout << from << ' ' << to;
	}
	cout << '\n';
	// int l = 1, r = inf;
	// auto check = [&](int x) -> bool {
	// 	DSU d(n);
	// 	for(auto [u, v, w] : edge){
	// 		if(w >= x){
	// 			if(!d.merge(u, v)){
	// 				return 1;
	// 			}
	// 		}
	// 	}
	// 	return 0;
	// };
	// while(l <= r){
	// 	int mid = (l + r) >> 1;
	// 	if(check(mid)) r = mid - 1;
	// 	else l = mid + 1;
	// }
	// DSU d(n);
	// for(auto [u, v, w] : edge){
	// 	if (w >= r){
	// 		if (!d.merge(u, v)){
				
	// 		}
	// 	}
	// }
}
    
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(9);
    
    int T = 1;
    cin >> T;
    while (T--){
        solve();
    }
    return 0;
}

题解:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
#define ll long long
#define pii pair<int, int>
#define ld long double
const int maxn = 1e6 + 5, inf = 1e9, maxm = 4e4 + 5, base = 37;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
// const int mod = 998244353;
// const __int128 mod = 212370440130137957LL;
int n, m;

struct DSU{
	int n;
	vector<int> fa, siz;
	DSU(int n) : n(n + 1), fa(n + 1), siz(n + 1, 1){
		iota(fa.begin(), fa.end(), 0);
	}
	int find(int x){
		if(x == fa[x]) return x;
		return fa[x] = find(fa[x]);
	}
	bool merge(int x, int y){
		x = find(x), y = find(y);
		if(x == y) return 0;
		fa[x] = y;
		return 1;
	}
};
//long long ? maxn ? n? m?
void solve(){
    ll res = 0;
    int k;
    cin >> n >> m;
	// vector<int> a(n + 1);
	// for(int i = 1; i <= n; i++){
	// 	cin >> a[i];
	// }
	vector<vector<pii>> G(n + 1);
	vector<array<int, 3>> edge;
	vector<int> vis(n + 1), ind(n + 1);
	for(int i = 1; i <= m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		edge.pb({w, u, v});
	}
	sort(edge.begin(), edge.end(), greater<>());
	int from, to, mn = inf;
	DSU d(n);
	for(auto [w, u, v] : edge){
		if(!d.merge(u, v)){
			from = u;
			to = v;
			mn = w;
		}
		else{
			G[u].pb({v, w});
			G[v].pb({u, w});
		}
	}
	
	bool ok = 0;
	vector<int> ans, path;
	auto dfs = [&](auto self, int u, int p) -> void {
		if(ok) return;
		if(u == to && p == from) return;
		if(vis[u]) return;
		vis[u] = 1;
		path.pb(u);
		if(u == to){
			ans = path;
			ok = 1;
			return;
		}
		for(auto [v, w] : G[u]){
			// if(vis[v] || u == from && v == to) continue;
			// vis[v] = 1;
			if(v == p) continue;
			self(self, v, u);
		}
		path.pop_back();
	};
	// vis[from] = 1;
	dfs(dfs, from, to);
	cout << mn << ' ' << ans.size() << '\n';
	for(auto x : ans){
		cout << x << ' ';
	}
	cout << '\n';
}
    
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(9);
    
    int T = 1;
    cin >> T;
    while (T--){
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__night_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值