답안 #343064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343064 2021-01-03T11:45:29 Z leinad2 Village (BOI20_village) C++17
50 / 100
164 ms 21548 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, i, j, k, a, b, ans[100010], A[100010], res, cent, sz[100010], dep[100010], in[100010], revin[100010], cnt, res2, ans2[100010];
vector<int>adj[100010];
void dfs(int v, int par, int depth)
{
    in[v]=++cnt;
    dep[v]=depth;
    sz[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs(p, v, depth+1);
        sz[v]+=sz[p];
    }
}
int centroid(int v, int par)
{
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        if(sz[p]*2>n)
        {
            return centroid(p, v);
        }
    }
    return v;
}
int f(int x)
{
    return ((x+(n/2))%n==0?n:(x+(n/2))%n);
}
queue<int>q;
void dfs2(int v, int par)
{
    if(adj[v].size()==1&&par==adj[v][0])
    {
        A[v]=1;
        swap(ans2[adj[v][0]], ans2[v]);
        res2+=2;
        return;
    }
    vector<int>V;
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs2(p, v);
        if(!A[p])
        {
            V.push_back(p);
        }
    }
    for(int i=0;i<V.size();i++)
    {
        swap(ans2[v], ans2[V[i]]);
        res2+=2;
    }
    if(ans2[v]!=v)A[v]=1;
}
main()
{
    for(scanf("%lld", &n);++i<n;)
    {
        scanf("%lld %lld", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1, 0, 0);
    cent=centroid(1, 0);
    cnt=0;
    dfs(cent, 0, 0);
    for(i=0;i++<n;)ans2[i]=i;
    dfs2(1, 0);
    if(ans2[1]=1)
    {
        swap(ans2[1], ans2[adj[1][0]]);
        res2+=2;
    }
    for(i=0;i++<n;)res+=dep[i]*2;
    for(i=0;i++<n;)
    {
        revin[in[i]]=i;
    }
    for(i=0;i++<n;)
    {
        ans[revin[i]]=revin[f(i)];
    }
    printf("%lld %lld\n", res2, res);
    for(i=0;i++<n;)printf("%lld ", ans2[i]);
    puts("");
    for(i=0;i++<n;)
    {
        printf("%lld ", ans[i]);
    }
}

Compilation message

Village.cpp: In function 'void dfs(long long int, long long int, long long int)':
Village.cpp:11:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp: In function 'long long int centroid(long long int, long long int)':
Village.cpp:21:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(long long int, long long int)':
Village.cpp:47:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp:57:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0;i<V.size();i++)
      |                 ~^~~~~~~~~
Village.cpp: At global scope:
Village.cpp:64:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   64 | main()
      |      ^
Village.cpp: In function 'int main()':
Village.cpp:78:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   78 |     if(ans2[1]=1)
      |        ~~~~~~~^~
Village.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |     for(scanf("%lld", &n);++i<n;)
      |         ~~~~~^~~~~~~~~~~~
Village.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |         scanf("%lld %lld", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 2688 KB Partially correct
2 Correct 2 ms 2668 KB Output is correct
3 Partially correct 2 ms 2668 KB Partially correct
4 Partially correct 2 ms 2668 KB Partially correct
5 Partially correct 3 ms 2668 KB Partially correct
6 Partially correct 3 ms 2668 KB Partially correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 3 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Partially correct 2 ms 2668 KB Partially correct
11 Partially correct 2 ms 2668 KB Partially correct
12 Partially correct 3 ms 2668 KB Partially correct
13 Partially correct 2 ms 2668 KB Partially correct
14 Correct 2 ms 2668 KB Output is correct
15 Partially correct 2 ms 2668 KB Partially correct
16 Partially correct 2 ms 2668 KB Partially correct
17 Partially correct 2 ms 2668 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Partially correct 3 ms 2796 KB Partially correct
6 Partially correct 3 ms 2796 KB Partially correct
7 Partially correct 3 ms 2796 KB Partially correct
8 Partially correct 3 ms 2796 KB Partially correct
9 Partially correct 3 ms 2796 KB Partially correct
10 Partially correct 3 ms 2796 KB Partially correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Partially correct 3 ms 2796 KB Partially correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 3 ms 2796 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Partially correct 2 ms 2796 KB Partially correct
24 Correct 2 ms 2796 KB Output is correct
25 Partially correct 2 ms 2796 KB Partially correct
26 Partially correct 3 ms 2796 KB Partially correct
27 Correct 3 ms 2796 KB Output is correct
28 Partially correct 3 ms 2796 KB Partially correct
29 Correct 2 ms 2796 KB Output is correct
30 Partially correct 3 ms 2796 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 2688 KB Partially correct
2 Correct 2 ms 2668 KB Output is correct
3 Partially correct 2 ms 2668 KB Partially correct
4 Partially correct 2 ms 2668 KB Partially correct
5 Partially correct 3 ms 2668 KB Partially correct
6 Partially correct 3 ms 2668 KB Partially correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 3 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Partially correct 2 ms 2668 KB Partially correct
11 Partially correct 2 ms 2668 KB Partially correct
12 Partially correct 3 ms 2668 KB Partially correct
13 Partially correct 2 ms 2668 KB Partially correct
14 Correct 2 ms 2668 KB Output is correct
15 Partially correct 2 ms 2668 KB Partially correct
16 Partially correct 2 ms 2668 KB Partially correct
17 Partially correct 2 ms 2668 KB Partially correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Partially correct 3 ms 2796 KB Partially correct
23 Partially correct 3 ms 2796 KB Partially correct
24 Partially correct 3 ms 2796 KB Partially correct
25 Partially correct 3 ms 2796 KB Partially correct
26 Partially correct 3 ms 2796 KB Partially correct
27 Partially correct 3 ms 2796 KB Partially correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Partially correct 3 ms 2796 KB Partially correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 3 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Partially correct 2 ms 2796 KB Partially correct
41 Correct 2 ms 2796 KB Output is correct
42 Partially correct 2 ms 2796 KB Partially correct
43 Partially correct 3 ms 2796 KB Partially correct
44 Correct 3 ms 2796 KB Output is correct
45 Partially correct 3 ms 2796 KB Partially correct
46 Correct 2 ms 2796 KB Output is correct
47 Partially correct 3 ms 2796 KB Partially correct
48 Partially correct 116 ms 12140 KB Partially correct
49 Correct 131 ms 13164 KB Output is correct
50 Correct 129 ms 13164 KB Output is correct
51 Partially correct 88 ms 10860 KB Partially correct
52 Partially correct 138 ms 13048 KB Partially correct
53 Correct 115 ms 12004 KB Output is correct
54 Partially correct 63 ms 11884 KB Partially correct
55 Partially correct 164 ms 21548 KB Partially correct
56 Correct 164 ms 17260 KB Output is correct
57 Partially correct 158 ms 15816 KB Partially correct
58 Partially correct 139 ms 14572 KB Partially correct
59 Partially correct 132 ms 13356 KB Partially correct
60 Correct 93 ms 13280 KB Output is correct
61 Correct 97 ms 13276 KB Output is correct
62 Correct 111 ms 13488 KB Output is correct
63 Correct 98 ms 12908 KB Output is correct
64 Correct 110 ms 13548 KB Output is correct
65 Correct 103 ms 13548 KB Output is correct
66 Correct 99 ms 12804 KB Output is correct
67 Correct 66 ms 10472 KB Output is correct
68 Correct 85 ms 12012 KB Output is correct
69 Correct 109 ms 13676 KB Output is correct
70 Correct 92 ms 13036 KB Output is correct
71 Correct 66 ms 10348 KB Output is correct
72 Partially correct 80 ms 11372 KB Partially correct
73 Correct 113 ms 13676 KB Output is correct
74 Correct 88 ms 12672 KB Output is correct
75 Correct 130 ms 13676 KB Output is correct
76 Correct 127 ms 12968 KB Output is correct
77 Correct 122 ms 13292 KB Output is correct
78 Correct 63 ms 9836 KB Output is correct
79 Partially correct 79 ms 11116 KB Partially correct
80 Correct 132 ms 13656 KB Output is correct
81 Correct 113 ms 13548 KB Output is correct
82 Correct 110 ms 13292 KB Output is correct