QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198967#6543. Visiting FriendZhou_JKWA 203ms68824kbC++232.7kb2023-10-03 19:46:052023-10-03 19:46:06

Judging History

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

  • [2023-10-03 19:46:06]
  • 评测
  • 测评结果:WA
  • 用时:203ms
  • 内存:68824kb
  • [2023-10-03 19:46:05]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int N=200005,M=500005;
int n,m,q;
struct Edge
{
    int to,next;
}edge[M*2];
int head[N],cnt;
void add_edge(int u,int v)
{
    cnt++;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
    return;
}
int dfn[N*2],low[N],Index;
int tot;
stack<int>s;
vector<int>G[N*2];
void tarjan(int u)
{
    dfn[u]=low[u]=++Index;
    s.emplace(u);
    for(int i=head[u];i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
            if(low[v]==dfn[u])
            {
                tot++;
                G[tot].clear();
                while(!s.empty()&&s.top()!=v)
                {
                    G[tot].emplace_back(s.top());
                    G[s.top()].emplace_back(tot);
                    s.pop();
                }
                G[tot].emplace_back(v);
                G[v].emplace_back(tot);
                s.pop();
                G[tot].emplace_back(u);
                G[u].emplace_back(tot);
            }
        }
        else low[u]=min(low[u],dfn[v]);
    }
    return;
}
int siz[N*2],c[N*2];
int fa[N*2][20],dep[N];
void dfs(int u,int father)
{
    dfn[u]=++Index;
    dep[u]=dep[father]+1;
    siz[u]=1;
    if(u<=n) c[u]=1;
    else c[u]=0;
    fa[u][0]=father;
    for(int i=1;(1<<i)<=tot;i++)
        fa[u][i]=fa[fa[u][i-1]][i-1];
    for(int v:G[u])
    {
        if(v==father) continue;
        dfs(v,u);
        siz[u]+=siz[v];
        c[u]+=c[v];
    }
    return;
}
int jump(int x,int t)
{
    int u=x;
    for(int i=0;(1<<i)<=tot;i++)
        if(t&(1<<i)) u=fa[u][i];
    return u;
}
void solve()
{
    cin>>n>>m;
    cnt=0;
    for(int i=1;i<=n;i++)
        G[i].clear(),head[i]=0;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        add_edge(x,y);
        add_edge(y,x);
    }
    Index=0,tot=n;
    fill(dfn+1,dfn+n+1,0);
    fill(low+1,low+n+1,0);
    while(!s.empty()) s.pop();
    tarjan(1);
    Index=0;
    dfs(1,0);
    cin>>q;
    while(q--)
    {
        int u,v;
        cin>>u>>v;
        int ans=n;
        if(dfn[u]<=dfn[v]&&dfn[v]<=dfn[u]+siz[u]-1) ans-=n-1-c[jump(v,dep[v]-dep[u]-1)];
        else ans-=c[u]-1;
        if(dfn[v]<=dfn[u]&&dfn[u]<=dfn[v]+siz[v]-1) ans-=n-1-c[jump(u,dep[u]-dep[v]-1)];
        else ans-=c[v]-1;
        cout<<ans<<"\n";
    }
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 23904kb

input:

1
5 5
1 2
1 3
2 4
4 5
2 5
5
1 2
1 4
2 3
2 5
3 5

output:

2
4
3
3
5

result:

ok 5 number(s): "2 4 3 3 5"

Test #2:

score: 0
Accepted
time: 2ms
memory: 21868kb

input:

100
10 25
7 4
1 10
7 2
3 4
5 7
9 10
10 5
8 10
6 7
9 1
4 2
2 6
8 5
6 9
5 9
7 1
2 1
4 1
9 8
8 3
1 8
4 8
2 10
3 1
3 6
100
6 4
10 8
5 4
7 8
3 10
5 9
6 9
6 8
2 10
10 9
6 9
1 8
3 6
10 9
1 4
10 8
1 6
5 1
5 10
9 1
3 5
8 7
3 2
3 9
2 9
9 4
2 10
8 4
6 2
2 1
7 4
3 6
6 5
10 6
1 4
5 1
7 10
7 1
8 5
3 8
7 5
3 9
5 4...

output:

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
...

result:

ok 10000 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 21920kb

input:

100
10 10
5 10
6 4
2 9
9 8
9 10
1 5
7 2
7 4
9 3
6 5
100
4 7
4 5
3 10
7 10
4 10
6 7
3 2
5 2
1 2
10 8
3 1
10 5
6 1
2 4
5 9
6 7
1 8
5 6
7 9
9 8
9 3
6 1
9 10
4 10
1 4
2 7
8 9
9 4
7 9
6 7
9 1
9 6
9 1
4 3
9 6
2 3
9 5
6 7
7 8
8 1
4 8
9 6
8 2
4 5
3 5
4 8
5 10
6 4
8 6
8 7
7 2
3 4
4 8
1 2
3 2
2 10
4 10
3 6
8 ...

output:

10
9
10
10
10
10
10
9
10
10
10
9
10
10
7
10
10
9
8
2
2
10
8
10
10
10
2
8
8
10
8
8
8
10
8
10
7
10
10
10
10
8
10
9
9
10
9
10
10
10
10
10
10
10
10
10
10
10
9
2
8
10
10
10
10
10
10
2
10
10
10
9
8
9
10
8
8
10
10
10
10
10
10
10
10
2
9
9
9
8
9
8
10
10
2
9
10
10
9
10
9
6
7
9
9
9
10
9
5
7
5
10
9
9
9
10
10
6
...

result:

ok 10000 numbers

Test #4:

score: 0
Accepted
time: 3ms
memory: 21868kb

input:

100
100 100
80 78
78 26
52 35
72 97
81 95
93 15
94 72
43 61
12 78
33 93
61 16
58 88
40 61
96 35
99 19
39 31
56 99
62 26
69 51
70 3
9 94
42 61
32 44
56 89
62 72
90 43
61 24
2 77
39 32
34 20
45 87
73 77
86 69
61 81
57 83
34 82
89 92
25 52
90 97
80 21
46 81
60 72
39 6
59 68
59 17
57 3
63 70
21 89
54 63...

output:

99
94
99
100
100
100
99
96
96
100
100
97
99
2
100
95
77
21
100
99
78
89
98
96
96
100
79
99
100
99
98
100
92
99
98
100
84
97
4
94
75
100
89
100
100
97
96
99
100
96
94
93
100
100
100
78
100
72
88
89
99
81
97
91
99
93
100
92
98
80
93
79
99
96
99
79
94
96
77
21
100
9
100
91
99
99
94
2
99
90
91
94
96
99
...

result:

ok 10000 numbers

Test #5:

score: 0
Accepted
time: 44ms
memory: 24044kb

input:

100
100 99
63 30
14 58
2 19
76 62
66 13
85 91
89 47
25 35
60 55
4 28
13 7
29 47
30 22
84 76
85 35
37 67
10 13
63 26
80 70
70 73
4 81
90 60
56 44
42 44
46 42
89 95
10 61
84 27
35 69
26 67
47 16
11 78
66 68
45 82
60 80
70 30
96 31
48 60
81 48
96 5
89 96
92 100
96 39
73 78
94 35
33 41
98 27
13 80
84 46...

output:

78
100
86
78
37
99
95
95
95
89
95
100
100
24
38
26
83
100
100
80
2
100
78
93
93
100
98
100
66
98
95
99
97
93
100
95
95
97
95
100
95
96
60
100
86
98
99
99
96
92
93
100
7
100
97
99
84
99
98
98
7
94
97
97
100
97
96
98
100
66
95
92
100
42
99
38
96
94
100
99
96
36
98
90
95
91
91
100
84
99
100
94
99
100
1...

result:

ok 300000 numbers

Test #6:

score: -100
Wrong Answer
time: 203ms
memory: 68824kb

input:

1
200000 200005
143434 88006
153771 154874
66423 170692
146283 6601
155961 164596
168635 18442
76196 80698
111864 77497
161981 64846
118578 76074
68608 192123
96367 47681
140827 119456
23398 117688
167600 79026
117829 18397
187735 145208
47404 38801
76057 195982
181616 66442
131190 175267
78809 1194...

output:

199996
199986
199987
200000
199998
198266
199998
199998
199976
200000
199998
199999
199991
199996
200000
199501
199999
200000
199986
199997
199999
200000
200000
199992
199952
200000
199993
199991
199993
199997
199914
199970
199998
199797
199040
199997
199974
199974
199934
199990
200000
199968
199995...

result:

wrong answer 16110th numbers differ - expected: '193731', found: '111'