QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577452#6888. TeyvatANewZhiyangfanWA 250ms142656kbC++143.8kb2024-09-20 11:34:202024-09-20 11:34:20

Judging History

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

  • [2024-09-20 11:34:20]
  • 评测
  • 测评结果:WA
  • 用时:250ms
  • 内存:142656kb
  • [2024-09-20 11:34:20]
  • 提交

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];
}
void solve()
{
    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;
	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]);
            }
        }
	}
}
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: 250ms
memory: 142656kb

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
92
9
-29
182
1
1
1
6
1
-29
-29
2
92
182
92
15
-29
3
440
271
182
-219
1
3
358
9
1
440
0
2119
182
15
92
2160
182
271
92
182
1
92
2
-29
440
2119
1
9
3
8
2160
-219
-29
9
6
2
2119
2160
358
182
9
358
358
271
358
271
92
440
271
2160
182
92
1
1
15
92
-64
2...

result:

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