QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#577455 | #6888. Teyvat | ANewZhiyangfan | WA | 4ms | 71276kb | C++14 | 3.9kb | 2024-09-20 11:36:04 | 2024-09-20 11:36:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 2e6+7;
struct node
{
LL y,next;
}e[2*N];
LL link[N],t=0,n,m,q;
void add(LL x,LL y)
{
e[++t].y=y;
e[t].next=link[x];
link[x]=t;
}
template <typename T>inline void read(T &x)
{
x=0;char c=getchar();bool f=0;
for(;c<'0'||c>'9';c=getchar())f|=(c=='-');
for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c-'0');
x=(f?-x:x);
}
stack<LL> st;
LL dfn[N],low[N];
vector<LL> Dcc[N];
LL num=0,cnt=0;
void tarjan(LL x)
{
dfn[x]=low[x]=++num;
st.push(x);
for(LL i=link[x];i;i=e[i].next)
{
LL y=e[i].y;
if(!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
if(low[y]>=dfn[x])
{
cnt++;
LL z;
do
{
z=st.top();
st.pop();
Dcc[cnt].push_back(z);
}while(z!=y);
Dcc[cnt].push_back(x);
}
}
else low[x]=min(low[x],dfn[y]);
}
}
LL dep[N],fa[N],siz[N],son[N],top[N],id[N];
LL F[N],S[N];
LL calc(LL x){return x*(x+1)/2;}
void dfs1(LL x,LL pre)
{
dep[x]=dep[pre]+1;
fa[x]=pre;
siz[x]=1;
S[x]=(x<=n);F[x]=calc(n);
for(LL i=link[x];i;i=e[i].next)
{
LL y=e[i].y;
if(y==pre) continue;
dfs1(y,x);
siz[x]+=siz[y];
S[x]+=S[y];
F[x]-=calc(S[y]);
if(siz[y]>siz[son[x]])son[x]=y;
}
F[x]-=calc(n-S[x]);
}
LL idx;
int seq[N];
void dfs2(LL x,LL topth)
{
seq[id[x]=++idx]=x;
top[x]=topth;
if(!son[x]) return;
dfs2(son[x],topth);
for(LL i=link[x];i;i=e[i].next)
{
LL y=e[i].y;
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
void construct()
{
t=0;
idx=n;
for(int i=1;i<=n;i++)link[i]=0;
for(LL i=1;i<=cnt;i++)
{
LL x=++idx;
for(LL j=0;j<Dcc[i].size();j++)
{
LL y=Dcc[i][j];
add(x,y);
add(y,x);
}
}
for(int i=1;i<=cnt;i++)Dcc[i].clear();
int U=idx;
idx=0;
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=U;i++)link[i]=0;t=0;
}
int qlca(LL x,LL y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
int dis(int x,int y){return dep[x]+dep[y]-2*dep[qlca(x,y)];}
bool pan(int x,int y,int z)
{
return dis(x,z)==dis(x,y)+dis(y,z);
}
int jmp(int x,int y)
{
while(top[x]!=top[y])
{
if(fa[top[y]]==x)return top[y];
y=fa[top[y]];
}
return seq[id[x]+1];
}
int RT=0;
void solve()
{
++RT;
read(n);read(m);read(q);
for(LL i=1;i<=m;i++)
{
LL x,y;read(x);read(y);
add(x,y);
add(y,x);
}
while(!st.empty())st.pop();
cnt=num=0;
for(int i=1;i<=n;i++)dfn[i]=low[i]=0;
tarjan(1);
construct();
while(q--)
{
int K;
read(K);
if(K==1)
{
int x;
read(x);
printf("%lld\n",F[x]);
}
else
{
int x,y;read(x);read(y);
bool flag=1;
for(int i=3;i<=K;i++)
{
int u;
read(u);
if(!flag)continue;
if(pan(x,u,y));
else if(pan(x,y,u))y=u;
else if(pan(u,x,y))x=u;
else flag=0;
}
if(!flag)printf("%d\n",0);
else
{
int t=qlca(x,y);
if(t==y)swap(x,y);
if(t==x)
{
assert(x!=y);
printf("%lld\n",1ll*S[y]*(n-S[jmp(x,y)]));
continue;
}
printf("%lld\n",1ll*S[x]*S[y]);
}
}
}
if(RT==2)exit(0);
}
int main()
{
int size(512<<20);// 512M
__asm__("movq %0, %%rsp\n"::"r"((char*)malloc(size)+size));
int test=0;
cin>>test;
while(test--)
{
solve();
}
exit(0);
return 0;
}
/*
3
3 3 3
1 3
3 2
1 2
1 1
2 2 3
3 1 2 3
3 3 3
1 3
3 2
1 2
1 1
2 2 3
3 1 2 3
3 3 3
1 3
3 2
1 2
1 1
2 2 3
3 1 2 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 71276kb
input:
1000 92 95 16 76 55 19 12 57 7 39 90 38 89 48 60 29 67 35 52 32 83 10 80 64 11 66 63 90 57 17 70 20 42 31 87 91 41 33 72 12 14 9 38 30 82 72 21 9 81 40 9 73 60 71 82 48 69 70 26 72 34 25 62 10 75 83 92 16 18 34 79 15 72 65 13 64 12 37 63 46 16 32 1 23 78 22 18 78 68 49 78 48 13 39 72 39 44 27 25 20 ...
output:
144 91 92 91 615 91 92 92 91 92 91 90 92 91 270 182 18 32 1 18 1 17 17 3 17 1 1 0 18 18 18 3 1 3 18 48 18 6 1 1 34 32 1 18 4 18 18 3 18 18 18 18 34 18 1 34 1 1 18 18 1 32 2 2 18 6 1 3 3 1 18 18 34 18 32 34 34
result:
wrong answer 78th lines differ - expected: '15', found: ''