QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577446#6888. TeyvatANewZhiyangfanWA 3ms50828kbC++143.7kb2024-09-20 11:30:422024-09-20 11:30:42

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 11:30:42]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:50828kb
  • [2024-09-20 11:30:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 1e6+7;
struct node
{
	LL y,next;
}e[2*N];
LL link[N],t=0,w[N],n,m,q,a[N];
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);
		}
	}
	idx=0;
	dfs1(1,0);
	dfs2(1,1);
}
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];
}
void solve()
{
    read(n);read(m);read(q);
    for(int i=1;i<=2*n;i++)link[i]=0;t=0;
	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();
	for(int i=1;i<=cnt;i++)Dcc[i].clear();cnt=num=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]);
            }
        }
	}
	exit(0);
}
int main()
{
    int test=0;
    cin>>test;
    while(test--)
    {
        solve();
    }
	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
*/

詳細信息

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 50828kb

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

result:

wrong answer 17th lines differ - expected: '18', found: ''