結果
問題 | No.2914 正閉路検出 |
ユーザー |
|
提出日時 | 2024-10-04 23:01:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 451 ms / 2,000 ms |
コード長 | 3,073 bytes |
コンパイル時間 | 2,291 ms |
コンパイル使用メモリ | 190,576 KB |
実行使用メモリ | 52,072 KB |
最終ジャッジ日時 | 2024-10-04 23:02:06 |
合計ジャッジ時間 | 13,332 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 55 |
コンパイルメッセージ
main.cpp: In function 'void dfs(long long int)': main.cpp:73:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 73 | for(auto [v,w]:g[x]) | ^
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long const int p=998244353; int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}} int inv(int x) {return po(x,p-2);} mt19937 rnd; #define app push_back #define all(x) (x).begin(),(x).end() #ifdef LOCAL #define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__) #define debugv(v) do {cout<< #v <<" : {"; for(int izxc=0;izxc<v.size();++izxc) {cout << v[izxc];if(izxc+1!=v.size()) cout << ","; }cout <<"}"<< endl;} while(0) #else #define debug(...) #define debugv(v) #endif #define lob(a,x) lower_bound(all(a),x) #define upb(a,x) upper_bound(all(a),x) const int maxn=1e5+5; map<pair<int,int>,int> ed;map<pair<int,int>,int> id; vector<pair<int,int> > g[maxn]; bool used[maxn]; int pr[maxn];int pot[maxn]; void solve2(vector<int> v) { int sum=0; for(int i=0;i<v.size();++i) { sum+=ed[{v[i],v[(i+1)%v.size()]}]; } if(sum<0) {sum*=(-1);reverse(all(v));} assert(sum); cout<<v.size()<<'\n'; cout<<v[0]+1<<'\n'; for(int i=0;i<v.size();++i) { cout<<id[{v[i],v[(i+1)%v.size()]}]+1;if(i+1!=v.size()) cout<<' '; } exit(0); } void solve(int x,int y) { debug(x,y); vector<int> vx,vy; vx.app(x);vy.app(y); while(pr[vx.back()]!=(-1)) {vx.app(pr[vx.back()]);} while(pr[vy.back()]!=(-1)) {vy.app(pr[vy.back()]);} debugv(vx);debugv(vy); map<int,int> posx;for(int i=0;i<vx.size();++i) {posx[vx[i]]=i;} vector<int> h; for(int i=0;i<vy.size();++i) { if(posx.count(vy[i])) { int j=posx[vy[i]]; for(int s=j;s>=0;--s) { h.app(vx[s]); } solve2(h); } else { h.app(vy[i]); } } assert(false); } void dfs(int x) { used[x]=true; for(auto [v,w]:g[x]) { if(!used[v]) { pr[v]=x; pot[v]=pot[x]+w; dfs(v); } else { if(pot[v]!=pot[x]+w) { solve(x,v); } } } } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); fill(pr,pr+maxn,-1); int n,m;cin>>n>>m; for(int i=0;i<m;++i) { int x,y,w;cin>>x>>y>>w;--x;--y; if(ed.count({x,y}) && ed[{x,y}]==w) { continue; } else if(ed.count({x,y})) { if(ed[{x,y}]-w>0) { cout<<2<<'\n'<<x+1<<'\n'<<id[{x,y}]+1<<' '<<i+1; return 0; } else { cout<<2<<'\n'<<x+1<<'\n'<<i+1<<' '<<id[{x,y}]+1; return 0; } } ed[{x,y}]=w;ed[{y,x}]=(-w); g[x].app({y,w});g[y].app({x,-w});id[{x,y}]=i;id[{y,x}]=i; } for(int i=0;i<n;++i) { if(!used[i]) {dfs(i);} } cout<<(-1); return 0; }