QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508113#3501. JailDimash0 40ms16636kbC++203.5kb2024-08-07 03:27:172024-08-07 03:27:17

Judging History

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

  • [2024-08-07 03:27:17]
  • 评测
  • 测评结果:0
  • 用时:40ms
  • 内存:16636kb
  • [2024-08-07 03:27:17]
  • 提交

answer

#include <bits/stdc++.h>

 
using namespace std;
    
typedef long long ll;
const int  N = 24e4 + 2, MOD = (int)1e9+7;

int n,x[N],y[N],m,it = 1,it1 = 1,s[N],f[N];
vector<int> g[N],G[N * 20];
int up[N][20],t[N][20],t1[N][20],tin[N],tout[N],timer = 0;
const int b = 18;
void dfs(int v,int pr) {
    tin[v] = ++timer;
    up[v][0] = pr;
    if(!x[v]) {
        t[v][0] = ++it;
    } else {
        t[v][0] = x[v];
    }
    if(!y[v]) {
        t1[v][0] = ++it;
    } else {
        t1[v][0] = y[v];
    }
    for(int i = 1;i <= b;i++) {
        up[v][i] = up[up[v][i-1]][i-1];
        t[v][i] = ++it;
        t1[v][i] = ++it;
        // cout << t[v][i] << ' ' << t[v][i - 1] << "\n" ;
        // cout << t[v][i] << ' ' << t[up[v][i-1]][i - 1] << '\n';
        G[t[v][i]].push_back(t[v][i-1]);G[t[v][i]].push_back(t[up[v][i-1]][i-1]);
        G[t1[v][i-1]].push_back(t1[v][i]);G[t1[up[v][i-1]][i-1]].push_back(t1[v][i]);
    }
    for(int to:g[v]) {
        if(to == pr) continue;
        dfs(to,v);
    }
    tout[v] = ++timer;
}
int vis[N * 20];
bool ans = 1;
void go(int v) {
    vis[v] = 1;
    for(int to:G[v]) {
        if(!vis[to]) {
            // cout << v << ' ' << to << '\n';
            go(to);
        } else {
            if(vis[to] == 1) {
                ans = 0;
            }
        }
    }
    vis[v] = 2;
}
bool is(int v,int u) {
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v,int u) {
    if(is(v,u)) return v;
    if(is(u,v)) return u;
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],u)) {
            v = up[v][i];
        }
    }
    return up[v][0];
}
void add(int v,int l,int id,bool ok = 1) {
    if(!is(l,v) || l == v) return;
    // cout << v << ' ' << l << ' ' << id << '\n';
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],l)) {
            // cout << id << ' ' << t[v][i] << '\n';
            G[id].push_back(t[v][i]);
            v = up[v][i];
        }
    }
    // cout << id << ' ' << t[v][1] << '\n';
    if(ok)G[id].push_back(t[v][1]);
    else G[id].push_back(t[v][0]);
}
void add_(int v,int l,int id,bool ok = 1) {
    v = up[v][0];
    if(!is(l,v) || l == v) return;
    // cout << v << ' ' << l << "x\n";
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],l)) {
            // cout << t1[v][i] << ' ' << id << '\n';
            G[t1[v][i]].push_back(id);
            v = up[v][i];
        }
    }
    // cout << t1[v][1] << ' ' << id << '\n';
    if(ok)G[t1[v][1]].push_back(id);
    else G[t1[v][0]].push_back(id);
}
void test() {
    ans = 1;
    timer = 0;
    cin >> n;
    for(int i = 1;i <= n;i++) {
        g[i].clear();
        x[i] = y[i] =0 ;
    }
    for(int i = 1;i <= n - 1;i++) {
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cin >> m;
    it = it1 = m;
    for(int i = 1;i <= m;i++) {
        cin >> s[i] >> f[i];
        y[f[i]] = x[s[i]] = i;
    }
    dfs(1,1);
    for(int i = 1;i <= m;i++) {
        int l = lca(s[i],f[i]);
        add(f[i],l,i,(s[i]!=l));add(up[s[i]][0],l,i,(s[i]!=l));
        add_(s[i],l,i,(f[i]!=l));add_(up[f[i]][0],l,i,(f[i]!=l));
    }
    for(int i = 1;i <= it;i++) {
        if(!vis[i]) {
            go(i);
        }
    }
    cout << (ans ? "Yes\n":"No\n");
    for(int i = 1;i <= it;i++) {
        G[i].clear();
        vis[i] =0;
    }
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1; 
    cin >> t;
    while(t--) {
        test();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 11828kb

input:

1
2
1 2
2
1 2
2 1

output:

Yes

result:

wrong answer 1st lines differ - expected: 'No', found: 'Yes'

Subtask #2:

score: 0
Wrong Answer

Test #21:

score: 5
Accepted
time: 9ms
memory: 16488kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
3 23
1 24
24 25
25 26
5 26
1 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
7 42
2 43
43 44
44 45
45 46
4 46
2 47
47 48
48 49
49 50
50 51
51 52
52 53
53 54
54 55
12 55
2 56
56 57
57 58
58 59
59 ...

output:

No
No
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
Yes
Yes

result:

ok 20 lines

Test #22:

score: 5
Accepted
time: 4ms
memory: 16540kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
3 23
1 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
5 32
1 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
7 41
2 42
42 43
43 44
44 45
45 46
46 47
4 47
2 48
48 49
49 50
50 51
51 52
52 53
53 54
12 54
2 55
55 56
56 57
57 58
58 59
14 ...

output:

Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes

result:

ok 20 lines

Test #23:

score: 5
Accepted
time: 8ms
memory: 12388kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
3 24
1 25
25 26
26 27
27 28
28 29
29 30
30 31
5 31
1 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
7 39
2 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
4 48
2 49
49 50
50 51
51 52
52 53
53 54
54 55
55 56
56 57
57 58
12 58
2 59
59 ...

output:

Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No

result:

ok 20 lines

Test #24:

score: 5
Accepted
time: 9ms
memory: 16552kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
3 20
1 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
5 31
1 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
7 49
2 50
50 51
51 52
52 53
53 54
54 55
55 56
56 57
57 58
4 58
2 59
59 60
60...

output:

No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes

result:

ok 20 lines

Test #25:

score: 5
Accepted
time: 4ms
memory: 16612kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
3 31
1 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
5 39
1 40
40 41
41 42
42 43
43 44
44 45
7 45
2 46
46 47
47 48
48 49
49 50
50 51
4 51
2 52
52 53
53 54
54 55
55 56
56 57
57 58
58 59
59 60
12...

output:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes

result:

ok 20 lines

Test #26:

score: 5
Accepted
time: 8ms
memory: 16636kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
3 20
1 21
21 22
22 23
23 24
24 25
25 26
26 27
5 27
1 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
7 37
2 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
4 46
2 47
47 48
12 48
2 49
49 50
50 51
14 51
3 52
52 53
53 54
54 55
55 56
56 57
57 58
58 5...

output:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes

result:

ok 20 lines

Test #27:

score: 5
Accepted
time: 8ms
memory: 16548kb

input:

20
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
3 21
1 22
22 23
23 24
24 25
5 25
1 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
7 39
2 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
4 47
2 48
48 49
49 50
50 51
51 52
52 53
53 54
54 55
55 56
12 56
2 57
57 58
58 59
59 ...

output:

Yes
No
No
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
No
Yes

result:

ok 20 lines

Test #28:

score: 5
Accepted
time: 5ms
memory: 16480kb

input:

17
250
1 15
15 16
16 17
17 18
18 19
19 20
20 21
3 21
1 22
22 23
23 24
24 25
25 26
26 27
27 28
5 28
1 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
7 39
2 40
40 41
41 42
4 42
2 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53
53 54
12 54
2 55
55 56
56 57
57 58
58 59
59 ...

output:

Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes

result:

ok 17 lines

Test #29:

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

input:

20
4
1 2
1 3
1 4
2
1 4
4 1
250
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 ...

output:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
No
Yes
Yes
Yes
Yes

result:

wrong answer 1st lines differ - expected: 'No', found: 'Yes'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Wrong Answer

Test #66:

score: 0
Wrong Answer
time: 40ms
memory: 16408kb

input:

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

output:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
No
Yes...

result:

wrong answer 25th lines differ - expected: 'No', found: 'Yes'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%