QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#55541 | #1288. Tokens on the Tree | Crysfly | WA | 38ms | 15336kb | C++11 | 3.7kb | 2022-10-14 14:38:13 | 2022-10-14 14:38:16 |
Judging History
answer
// what is matter? never mind.
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
//#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 1000000007
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 500005
#define inf 0x3f3f3f3f
int n,fa[maxn],rt,lim;
vi e[maxn];
struct node{
int x,y,z;
void add(int o){
if(o>x)z=y,y=x,x=o;
else if(o>y)z=y,y=o;
else if(o>z)z=o;
}
}mx[maxn];
int sz[maxn],mxp[maxn];
void getrt(int u,int pa){
fa[u]=pa,sz[u]=1,mxp[u]=0,mx[u].x=mx[u].y=mx[u].z=0;
for(int v:e[u])
if(v!=pa)getrt(v,u),sz[u]+=sz[v],mx[u].add(sz[v]);
mx[u].add(n-sz[u]);
mxp[u]=mx[u].x;
if(mxp[u]<mxp[rt])rt=u;
}
int o[maxn],len;
priority_queue<pii,vector<pii>,greater<pii> >q;
modint res=0;
int orz;
int tr[maxn];
void add(int x,int y){
for(;x;x^=x&-x)tr[x]+=y;
}
int ask(int x){
int res=0;
for(;x<=n;x+=x&-x)res+=tr[x];
return res;
}
bool pd(int i,int j){
if(i<j)swap(i,j);
For(u,1,n)
if(i<=mx[u].y && j<=mx[u].z) return 1;
return 0;
}
void work()
{
n=read();
For(i,1,n) e[i].clear(),tr[i]=0,mx[i].x=mx[i].y=mx[i].z=0; len=0; while(q.size())q.pop(); res=0; orz=0;
For(i,2,n){
int v=read();
e[i].pb(v),e[v].pb(i);
}
if(n==2){
puts("2");
return;
}
mxp[0]=n+1;
getrt(1,0),getrt(rt,0);
For(i,1,n)orz=max(orz,min(mx[i].x,n-mx[i].x-mx[i].y));
for(auto v:e[rt])o[++len]=sz[v];
sort(o+1,o+len+1,greater<int>());
lim=o[1];
q.push(mkp(mxp[rt],rt));
For(i,lim+1,n-1){
while(q.size() && q.top().fi<i){
int u=q.top().se; q.pop();
if(u!=rt) add(sz[u],-1);
for(auto v:e[u])
if(v!=fa[u])q.push(mkp(mxp[v],v)),add(sz[v],1);
}
For(j,1,i)
if(i+j<=n)
res+=1ll*i*j*(1+(i!=j))%mod*ask(j)%mod;
}
For(i,1,lim)
For(j,1,lim)
res+=(pd(i,j)?1ll:2ll)*i*j%mod;
cout<<res.x<<"\n";
}
signed main()
{
int T=read();
while(T--)work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 15336kb
input:
2 5 1 2 3 3 10 1 2 3 4 3 6 3 8 2
output:
71 989
result:
ok 2 tokens
Test #2:
score: -100
Wrong Answer
time: 38ms
memory: 15316kb
input:
33327 10 1 2 3 4 5 2 7 8 2 7 1 2 3 4 5 2 8 1 2 3 4 5 3 1 2 1 7 1 2 3 3 3 1 5 1 2 2 1 5 1 2 3 1 2 1 4 1 2 2 8 1 2 3 4 5 5 4 9 1 2 3 1 5 6 7 8 5 1 1 3 4 8 1 2 3 3 5 5 5 8 1 2 2 2 1 6 1 6 1 2 3 2 5 7 1 2 3 4 3 2 5 1 2 3 3 4 1 1 1 6 1 1 3 4 4 3 1 2 10 1 2 1 4 5 5 7 7 9 2 1 3 1 2 6 1 1 3 4 5 7 1 2 3 1 5 ...
output:
981 285 565 2 307 71 70 2 31 927 660 70 419 419 141 303 95 31 229 18 1749 2 18 220 252 18 95 2 2 765 1557 4023 199 70 450 591 72 1627 18 4041 2587 200 1611 10 18 88 71 877 449 95 303 1607 72 474 2 72 1563 303 711 933 18 919 2 2583 449 1725 30 449 417 10 2 423 449 72 667 2583 1563 95 913 877 18 140 7...
result:
wrong answer 2nd words differ - expected: '253', found: '285'