1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * ޱ BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } struct node{ signed s,t1,t2; node operator +(node a){ return {s^a.s,s? t1+a.t2:t1+a.t1,s? t2+a.t1:t2+a.t2}; } void operator ^=(node a){ s^=a.s; t1=s==0;t2=s==1; } int val(){return 1ll*t1*t2%MOD;} }; int n; struct ds{ int m,op; node seg[3000005]; signed ls[3000005],rs[3000005]; signed rt[100005],tot; void upd(int pos,int l,int r,signed &p){ if(!p)p=tot++,ls[p]=rs[p]=0,seg[p]={0,0,(signed)(r-l+1)}; if(l==r){ seg[p]^={1,0,1}; return; } int m=(l+r)>>1; if(m>=pos)upd(pos,l,m,ls[p]); else upd(pos,m+1,r,rs[p]); if(!ls[p])seg[p]=seg[rs[p]],seg[p].t1+=m-l+1; else if(!rs[p])seg[p]=seg[ls[p]],(seg[p].s? seg[p].t2:seg[p].t1)+=r-m; else seg[p]=seg[ls[p]]+seg[rs[p]]; } void merge(int l,int r,int p,int p2){ if(l==r){ seg[p]^=seg[p2]; return; } int m=(l+r)>>1; if(!ls[p])ls[p]=ls[p2]; else if(ls[p2])merge(l,m,ls[p],ls[p2]); if(!rs[p])rs[p]=rs[p2]; else if(rs[p2])merge(m+1,r,rs[p],rs[p2]); if(!ls[p])seg[p]=seg[rs[p]],seg[p].t1+=m-l+1; else if(!rs[p])seg[p]=seg[ls[p]],(seg[p].s? seg[p].t2:seg[p].t1)+=r-m; else seg[p]=seg[ls[p]]+seg[rs[p]]; } void init(int x=0,int o=0){ op=o;m=x; if(m%2!=op)--m; REP(i,0,n)rt[i]=0; tot=1; } void update(int p,int pos){ ++pos; if(pos%2!=op)++pos; if(pos>m)return; upd(pos/2,0,m/2,rt[p]); } void mrg(int x,int y){ if(rt[x])merge(0,m/2,rt[x],rt[y]); else rt[x]=rt[y]; } int calc(int x){return seg[rt[x]].val();} }s1,s2; vector<pii>v[100005]; vector<int>add[100005]; int m; int ans; void dfs(int x,int pre,int pv){ for(auto [i,j]:v[x])if(i!=pre){ dfs(i,x,j); s1.mrg(x,i);s2.mrg(x,i); } for(auto i:add[x])s1.update(x,i),s2.update(x,i); if(x){ (ans+=1ll*pv*(s1.calc(x)+s2.calc(x)))%=MOD; } } void Main() { cin>>n>>m; REP(i,1,n){ int x,y,w; cin>>x>>y>>w; --x,--y; v[x].pb({y,w});v[y].pb({x,w}); } s1.init(m,0);s2.init(m,1); REP(i,0,m){ int x;cin>>x; add[x-1].pb(i); } dfs(0,-1,0); over(ans) } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("ship.in","r",stdin); freopen("ship.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3063
- Time
- 700ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By