結果
問題 | No.3104 Simple Graph Problem |
ユーザー |
|
提出日時 | 2025-04-11 22:01:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 2,496 bytes |
コンパイル時間 | 2,177 ms |
コンパイル使用メモリ | 179,204 KB |
実行使用メモリ | 72,328 KB |
最終ジャッジ日時 | 2025-04-11 22:01:53 |
合計ジャッジ時間 | 14,757 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 65 |
コンパイルメッセージ
main.cpp: In function ‘int32_t main()’: main.cpp:68:14: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions] 68 | for(auto [x,y]:ed) | ^ main.cpp:95:14: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions] 95 | for(auto [key,val]:add) | ^
ソースコード
#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=1e6+5; int c[maxn]; map<pair<int,int>,int> add; vector<int> g[maxn]; bool used[maxn];int col[maxn];int s=0; void dfs(int x) { used[x]=true; if(!col[x]) {s+=c[x];s%=p;} else {s-=c[x];s%=p;} for(int v:g[x]) { if(!used[v]) { col[v]=(col[x]^1); dfs(v); } } } const int inv2=(p+1)/2; void dfs2(int x) { used[x]=true; for(int v:g[x]) { if(!used[v]) { dfs2(v); add[{x,v}]-=c[v];add[{x,v}]%=p; c[x]-=c[v];c[x]%=p;c[v]=0; } } } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m;cin>>n>>m; for(int i=0;i<n;++i) {cin>>c[i];} vector<pair<int,int> > ed;vector<int> res(m); map<pair<int,int>,int> id; for(int i=0;i<m;++i) { int x,y;cin>>x>>y;--x;--y; ed.app({x,y}); g[x].app(y);g[y].app(x); id[{x,y}]=i;id[{y,x}]=i; } dfs(0); for(auto [x,y]:ed) { if(col[x]==col[y]) { if(col[x]==0) { add[{x,y}]+=(-s*inv2)%p; c[x]-=(s*inv2)%p; c[y]-=(s*inv2)%p; } else { add[{x,y}]+=(s*inv2)%p; c[x]+=(s*inv2)%p; c[y]+=(s*inv2)%p; } s=0; break; } } if(s) { cout<<(-1);return 0; } fill(used,used+n,false); dfs2(0); for(int i=0;i<n;++i) debug(i,c[i]); for(auto [key,val]:add) { res[id[key]]+=val;res[id[key]]%=p;res[id[key]]+=p;res[id[key]]%=p; } for(int i=0;i<m;++i) { cout<<(p-res[i])%p<<' '; } return 0; }