結果
問題 | No.1507 Road Blocked |
ユーザー | mugen_1337 |
提出日時 | 2021-05-14 22:58:38 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 641 ms / 2,000 ms |
コード長 | 6,239 bytes |
コンパイル時間 | 2,947 ms |
コンパイル使用メモリ | 228,908 KB |
実行使用メモリ | 77,236 KB |
最終ジャッジ日時 | 2024-04-10 02:15:48 |
合計ジャッジ時間 | 22,460 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 401 ms
76,364 KB |
testcase_04 | AC | 632 ms
76,576 KB |
testcase_05 | AC | 611 ms
76,828 KB |
testcase_06 | AC | 613 ms
76,580 KB |
testcase_07 | AC | 592 ms
76,456 KB |
testcase_08 | AC | 619 ms
77,236 KB |
testcase_09 | AC | 627 ms
76,616 KB |
testcase_10 | AC | 641 ms
76,996 KB |
testcase_11 | AC | 637 ms
76,500 KB |
testcase_12 | AC | 622 ms
76,592 KB |
testcase_13 | AC | 624 ms
76,644 KB |
testcase_14 | AC | 617 ms
76,588 KB |
testcase_15 | AC | 605 ms
76,596 KB |
testcase_16 | AC | 620 ms
76,876 KB |
testcase_17 | AC | 626 ms
76,464 KB |
testcase_18 | AC | 638 ms
77,104 KB |
testcase_19 | AC | 629 ms
76,624 KB |
testcase_20 | AC | 621 ms
76,556 KB |
testcase_21 | AC | 636 ms
76,592 KB |
testcase_22 | AC | 625 ms
76,400 KB |
testcase_23 | AC | 618 ms
76,536 KB |
testcase_24 | AC | 621 ms
76,684 KB |
testcase_25 | AC | 626 ms
76,504 KB |
testcase_26 | AC | 613 ms
76,328 KB |
testcase_27 | AC | 622 ms
76,640 KB |
testcase_28 | AC | 626 ms
76,640 KB |
testcase_29 | AC | 613 ms
76,460 KB |
testcase_30 | AC | 627 ms
76,400 KB |
testcase_31 | AC | 630 ms
76,456 KB |
testcase_32 | AC | 622 ms
76,504 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } template<ll Mod> struct ModInt{ long long x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=Mod) x-=Mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=Mod-p.x)>=Mod)x-=Mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1ll*x*p.x%Mod); return *this; } ModInt &operator/=(const ModInt &p){ (*this)*=p.inverse(); return *this; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} bool operator==(const ModInt &p)const{return x==p.x;} bool operator!=(const ModInt &p)const{return x!=p.x;} ModInt inverse()const{ int a=x,b=Mod,u=1,v=0,t; while(b>0){ t=a/b; swap(a-=t*b,b);swap(u-=t*v,v); } return ModInt(u); } ModInt pow(long long n)const{ ModInt ret(1),mul(x); while(n>0){ if(n&1) ret*=mul; mul*=mul;n>>=1; } return ret; } friend ostream &operator<<(ostream &os,const ModInt &p){return os<<p.x;} friend istream &operator>>(istream &is,ModInt &a){long long t;is>>t;a=ModInt<Mod>(t);return (is);} static int get_mod(){return Mod;} }; // 経路圧縮なし,マージテクでunite,クエリ毎O(logN) struct UnionFindUndo{ int con; vector<int> data; stack<tuple<int,int,bool>> history; UnionFindUndo(int sz){ con=sz; data.assign(sz,-1); } bool unite(int x,int y){ x=root(x),y=root(y); if(x==y){ history.push(make_tuple(x,data[x],false)); history.push(make_tuple(y,data[y],false)); return false; } con--; history.push(make_tuple(x,data[x],true)); history.push(make_tuple(y,data[y],false)); if(data[x]>data[y]) swap(x,y); data[x]+=data[y]; data[y]=x; return true; } int root(int k){return (data[k]<0?k:(root(data[k])));} int size(int k){return (-data[root(k)]);} bool sameroot(int x,int y){return root(x)==root(y);} void undo(){ { auto [fs,sc,con_flag]=history.top();history.pop(); if(con_flag) con++; data[fs]=sc; } { auto [fs,sc,con_flag]=history.top();history.pop(); if(con_flag) con++; data[fs]=sc; } } void snapshot(){while(!history.empty())history.pop();} void rollback(){while(!history.empty())undo();} }; // verified https://codeforces.com/gym/100551/submission/103148848 // ref https://ei1333.hateblo.jp/entry/2017/12/14/000000 struct DynamicConnectivityOffline{ private: int sz; using P=pair<int,int>; vector<vector<P>> seg; vector<pair<P,P>> es;// {{start idx, end idx}, {u, v}} map<P,int> cnt,prev; void add_seg(int a,int b,P &e,int k,int l,int r){ if(r<=a or b<=l) return ; if(a<=l and r<=b){ seg[k].push_back(e); return ; } add_seg(a,b,e,2*k,l,(l+r)/2); add_seg(a,b,e,2*k+1,(l+r)/2,r); } public: int n,q; UnionFindUndo uf; DynamicConnectivityOffline(int n,int q):n(n),q(q),uf(n){ sz=1;while(sz<q)sz<<=1; seg.resize(sz*2); } // queryのindex順に追加 void add_edge(int query_idx,int u,int v){ if(u>v) swap(u,v); if(cnt[P(u,v)]==0) prev[P(u,v)]=query_idx; cnt[P(u,v)]++; } void delete_edge(int query_idx,int u,int v){ if(u>v) swap(u,v); assert(cnt[P(u,v)]>0); cnt[P(u,v)]--; if(cnt[P(u,v)]==0) es.emplace_back(P(prev[P(u,v)],query_idx),P(u,v)); } void build(){ for(auto [e,c]:cnt)if(c>0) es.emplace_back(P(prev[e],sz),e); for(auto [t,e]:es) add_seg(t.first,t.second,e,1,0,sz); } // dfs -> segment tree // f(query_idx) void execute(const function<void(int)> &f,int k=1){ for(auto [u,v]:seg[k]) uf.unite(u,v); if(k<sz){ execute(f,2*k); execute(f,2*k+1); } else if(k-sz<q) f(k-sz);// query for(auto [u,v]:seg[k]) uf.undo(); } }; using mint=ModInt<998244353>; mint i2=mint(2).inverse(); signed main(){ int N;cin>>N; DynamicConnectivityOffline daicon(N,3*N-3); vector<pair<int,int>> E; rep(i,N-1){ int u,v;cin>>u>>v; E.emplace_back(u,v); daicon.add_edge(i,u,v); } rep(i,N-1){ auto [u,v]=E[i]; daicon.delete_edge(N-1+2*i,u,v); daicon.add_edge(N-1+2*i+1,u,v); } daicon.build(); mint num=0; auto f=[&](int k){ if(k<N-1) return ; if((k-N+1)%2) return ; int A=daicon.uf.size(0); int B=N-A; num+=mint(A)*mint(A-1)*i2; num+=mint(B)*mint(B-1)*i2; return ; }; daicon.execute(f); mint den=mint(N)*mint(N-1)*mint(N-1)*i2; cout<<num/den<<endl; return 0; }