結果
| 問題 |
No.2914 正閉路検出
|
| コンテスト | |
| ユーザー |
n0ma_ru
|
| 提出日時 | 2024-10-05 19:00:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,736 bytes |
| コンパイル時間 | 2,434 ms |
| コンパイル使用メモリ | 210,524 KB |
| 最終ジャッジ日時 | 2025-02-24 15:59:46 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 50 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
int n,m;
vector<array<int,4>> e;
struct WUF {
vector<int> par, rank;
vector<ll> w;
int n;
WUF(int n) : n(n), par(n), rank(n), w(n) {
iota(ALL(par),0);
}
int root(int x) {
if (x == par[x]) return x;
return par[x] = root(par[x]);
}
bool same(int x, int y) {
return (root(x) == root(y));
}
bool unite(int x, int y, ll dw) {
x = root(x);
y = root(y);
dw += w[x];
dw -= w[y];
if (x == y) return false;
if (rank[x] > rank[y]) swap(x,y), dw = -dw;
if (rank[x] == rank[y]) ++rank[x];
par[x] = y;
w[y] = dw;
return true;
}
ll weight(int x) {
return w[x];
}
ll diff(int x, int y) {
return weight(y) - weight(x);
}
};
int main() {
cin >> n >> m;
e.resize(m);
for (int i = 0; i < m; ++i) {
auto& [u,v,w,id] = e[i];
cin >> u >> v >> w;
--u; --v;
id = i;
}
vector<vector<pair<int,int>>> g(n);
WUF uf(n);
for (auto&& [u,v,w,i] : e) {
// check;
// for (int j = 0; j < n; ++j) {
// cerr << uf.weight(j) << " \n"[j==n-1];
// }
if (uf.same(u,v)) {
if (uf.diff(u,v) == w) {
/// ok
}
else {
// bfs
queue<int> q;
vector<pair<int,int>> pre(n, {-1,-1});
q.push(u);
pre[u] = {-2, -2};
while (q.size()) {
int pos = q.front(); q.pop();
for (auto [to,id] : g[pos]) {
if (pre[to].first == -1) {
pre[to].first = pos;
pre[to].second = id;
q.push(to);
}
}
}
vector<int> path;
int now = v;
while (pre[now].second != -2) {
path.push_back(pre[now].second);
now = pre[now].first;
}
path.push_back(i);
// out
cout << v+1 << '\n';
cout << path.size() << '\n';
for (int j = 0; j < path.size(); ++j) {
cout << path[j]+1 << " \n"[j==path.size()-1];
}
return 0;
}
}
else {
uf.unite(u, v, w);
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
}
cout << "-1\n";
}
n0ma_ru