結果
| 問題 |
No.1507 Road Blocked
|
| コンテスト | |
| ユーザー |
mugen_1337
|
| 提出日時 | 2021-05-14 22:58:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 780 ms / 2,000 ms |
| コード長 | 6,239 bytes |
| コンパイル時間 | 3,083 ms |
| コンパイル使用メモリ | 225,756 KB |
| 最終ジャッジ日時 | 2025-01-21 11:59:25 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#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;
}
mugen_1337