Submission #343029

# Submission time Handle Problem Language Result Execution time Memory
343029 2021-01-03T11:18:15 Z urd05 Village (BOI20_village) C++14
50 / 100
148 ms 24300 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> adj[100000];
vector<int> son[100000];
int dp0[100000];
int dp1[100000];
int p[100000];
int group[100000];
int save[100000];
int nxt[100000];
int nxt2[100000];

void dfs(int v,int prev) {
    p[v]=prev;
    for(int i=0;i<adj[v].size();i++) {
        int nt=adj[v][i];
        if (nt!=prev) {
            son[v].push_back(nt);
            dfs(nt,v);
        }
    }
    for(int i=0;i<son[v].size();i++) {
        int nt=son[v][i];
        dp1[v]+=max(dp0[nt],dp1[nt]);
    }
    int mx=-1e6;
    dp0[v]=1;
    for(int i=0;i<son[v].size();i++) {
        int nt=son[v][i];
        mx=max(mx,dp1[nt]-dp0[nt]);
        dp0[v]+=max(dp0[nt],dp1[nt]);
    }
    if (mx<0) {
        dp0[v]+=mx;
    }
}

int n;

bool visited[100000];
bool gone[100000];
vector<int> v;
int dist[100000];

int bfs(int u) {
    memset(visited,0,sizeof(visited));
    dist[u]=0;
    queue<int> q;
    q.push(u);
    visited[u]=true;
    while (!q.empty()) {
        int now=q.front();
        q.pop();
        for(int i=0;i<adj[now].size();i++) {
            int nt=adj[now][i];
            if (!gone[nt]&&!visited[nt]) {
                visited[nt]=true;
                q.push(nt);
                dist[nt]=dist[now]+1;
            }
        }
    }
    int pos=u;
    for(int i=0;i<n;i++) {
        if (!gone[i]&&dist[i]>dist[pos]) {
            pos=i;
        }
    }
    return pos;
}

void dfs2(int v,int type) {
    if (type==0) {
        group[v]=v;
        save[v]=v;
        int pos=-1;
        int mx=-1e6;
        for(int i=0;i<son[v].size();i++) {
            int nt=son[v][i];
            if (dp1[nt]-dp0[nt]>mx) {
                mx=dp1[nt]-dp0[nt];
                pos=i;
            }
        }
        for(int i=0;i<son[v].size();i++) {
            int nt=son[v][i];
            if (dp1[nt]>dp0[nt]||pos==i) {
                dfs2(nt,1);
            }
            else {
                dfs2(nt,0);
            }
        }
    }
    else {
        group[v]=group[p[v]];
        nxt[v]=save[group[v]];
        save[group[v]]=v;
        for(int i=0;i<son[v].size();i++) {
            int nt=son[v][i];
            if (dp1[nt]>dp0[nt]) {
                dfs2(nt,1);
            }
            else {
                dfs2(nt,0);
            }
        }
    }
}

int main(void) {
    memset(save,-1,sizeof(save));
    scanf("%d",&n);
    for(int i=1;i<n;i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0,-1);
    printf("%d ",2*n-2*dp0[0]);
    int now=bfs(0);
    /*
    v.push_back(now);
    int ret=0;
    int st=now;
    for(int cnt=0;cnt+1<n;cnt++) {
        int save=now;
        now=bfs(now);
        gone[save]=true;
        v.push_back(now);
        ret+=dist[now];
    }
    memset(gone,0,sizeof(gone));
    bfs(now);
    ret+=dist[st];*/
    printf("1\n");
    dfs2(0,0);
    for(int i=0;i<n;i++) {
        if (save[i]!=-1) {
            nxt[i]=save[i];
        }
    }
    for(int i=0;i<n;i++) {
        printf("%d ",nxt[i]+1);
    }
    printf("\n");
    for(int i=0;i<n;i++) {
        printf("%d ",nxt2[i]+1);
    }
}

Compilation message

Village.cpp: In function 'void dfs(int, int)':
Village.cpp:16:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for(int i=0;i<adj[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
Village.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for(int i=0;i<son[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
Village.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i=0;i<son[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
Village.cpp: In function 'int bfs(int)':
Village.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for(int i=0;i<adj[now].size();i++) {
      |                     ~^~~~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(int, int)':
Village.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for(int i=0;i<son[v].size();i++) {
      |                     ~^~~~~~~~~~~~~~
Village.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         for(int i=0;i<son[v].size();i++) {
      |                     ~^~~~~~~~~~~~~~
Village.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |         for(int i=0;i<son[v].size();i++) {
      |                     ~^~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:125:9: warning: unused variable 'now' [-Wunused-variable]
  125 |     int now=bfs(0);
      |         ^~~
Village.cpp:114:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  114 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Village.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  117 |         scanf("%d %d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 5484 KB Partially correct
2 Partially correct 5 ms 5484 KB Partially correct
3 Partially correct 6 ms 5484 KB Partially correct
4 Partially correct 5 ms 5484 KB Partially correct
5 Partially correct 4 ms 5484 KB Partially correct
6 Partially correct 5 ms 5484 KB Partially correct
7 Partially correct 4 ms 5484 KB Partially correct
8 Partially correct 4 ms 5484 KB Partially correct
9 Partially correct 4 ms 5484 KB Partially correct
10 Partially correct 4 ms 5484 KB Partially correct
11 Partially correct 4 ms 5484 KB Partially correct
12 Partially correct 4 ms 5484 KB Partially correct
13 Partially correct 5 ms 5484 KB Partially correct
14 Partially correct 4 ms 5484 KB Partially correct
15 Partially correct 4 ms 5484 KB Partially correct
16 Partially correct 3 ms 5484 KB Partially correct
17 Partially correct 4 ms 5484 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 5612 KB Partially correct
2 Partially correct 4 ms 5616 KB Partially correct
3 Partially correct 4 ms 5612 KB Partially correct
4 Partially correct 4 ms 5612 KB Partially correct
5 Partially correct 4 ms 5612 KB Partially correct
6 Partially correct 5 ms 5612 KB Partially correct
7 Partially correct 5 ms 5740 KB Partially correct
8 Partially correct 6 ms 5612 KB Partially correct
9 Partially correct 5 ms 5612 KB Partially correct
10 Partially correct 5 ms 5612 KB Partially correct
11 Partially correct 4 ms 5612 KB Partially correct
12 Partially correct 4 ms 5612 KB Partially correct
13 Partially correct 6 ms 5612 KB Partially correct
14 Partially correct 5 ms 5612 KB Partially correct
15 Partially correct 5 ms 5612 KB Partially correct
16 Partially correct 4 ms 5612 KB Partially correct
17 Partially correct 4 ms 5612 KB Partially correct
18 Partially correct 5 ms 5612 KB Partially correct
19 Partially correct 4 ms 5612 KB Partially correct
20 Partially correct 5 ms 5612 KB Partially correct
21 Partially correct 4 ms 5612 KB Partially correct
22 Partially correct 5 ms 5612 KB Partially correct
23 Partially correct 6 ms 5612 KB Partially correct
24 Partially correct 4 ms 5612 KB Partially correct
25 Partially correct 4 ms 5612 KB Partially correct
26 Partially correct 6 ms 5612 KB Partially correct
27 Partially correct 4 ms 5612 KB Partially correct
28 Partially correct 5 ms 5612 KB Partially correct
29 Partially correct 5 ms 5612 KB Partially correct
30 Partially correct 6 ms 5612 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 5484 KB Partially correct
2 Partially correct 5 ms 5484 KB Partially correct
3 Partially correct 6 ms 5484 KB Partially correct
4 Partially correct 5 ms 5484 KB Partially correct
5 Partially correct 4 ms 5484 KB Partially correct
6 Partially correct 5 ms 5484 KB Partially correct
7 Partially correct 4 ms 5484 KB Partially correct
8 Partially correct 4 ms 5484 KB Partially correct
9 Partially correct 4 ms 5484 KB Partially correct
10 Partially correct 4 ms 5484 KB Partially correct
11 Partially correct 4 ms 5484 KB Partially correct
12 Partially correct 4 ms 5484 KB Partially correct
13 Partially correct 5 ms 5484 KB Partially correct
14 Partially correct 4 ms 5484 KB Partially correct
15 Partially correct 4 ms 5484 KB Partially correct
16 Partially correct 3 ms 5484 KB Partially correct
17 Partially correct 4 ms 5484 KB Partially correct
18 Partially correct 4 ms 5612 KB Partially correct
19 Partially correct 4 ms 5616 KB Partially correct
20 Partially correct 4 ms 5612 KB Partially correct
21 Partially correct 4 ms 5612 KB Partially correct
22 Partially correct 4 ms 5612 KB Partially correct
23 Partially correct 5 ms 5612 KB Partially correct
24 Partially correct 5 ms 5740 KB Partially correct
25 Partially correct 6 ms 5612 KB Partially correct
26 Partially correct 5 ms 5612 KB Partially correct
27 Partially correct 5 ms 5612 KB Partially correct
28 Partially correct 4 ms 5612 KB Partially correct
29 Partially correct 4 ms 5612 KB Partially correct
30 Partially correct 6 ms 5612 KB Partially correct
31 Partially correct 5 ms 5612 KB Partially correct
32 Partially correct 5 ms 5612 KB Partially correct
33 Partially correct 4 ms 5612 KB Partially correct
34 Partially correct 4 ms 5612 KB Partially correct
35 Partially correct 5 ms 5612 KB Partially correct
36 Partially correct 4 ms 5612 KB Partially correct
37 Partially correct 5 ms 5612 KB Partially correct
38 Partially correct 4 ms 5612 KB Partially correct
39 Partially correct 5 ms 5612 KB Partially correct
40 Partially correct 6 ms 5612 KB Partially correct
41 Partially correct 4 ms 5612 KB Partially correct
42 Partially correct 4 ms 5612 KB Partially correct
43 Partially correct 6 ms 5612 KB Partially correct
44 Partially correct 4 ms 5612 KB Partially correct
45 Partially correct 5 ms 5612 KB Partially correct
46 Partially correct 5 ms 5612 KB Partially correct
47 Partially correct 6 ms 5612 KB Partially correct
48 Partially correct 113 ms 12856 KB Partially correct
49 Partially correct 120 ms 13548 KB Partially correct
50 Partially correct 140 ms 13452 KB Partially correct
51 Partially correct 90 ms 11756 KB Partially correct
52 Partially correct 128 ms 13420 KB Partially correct
53 Partially correct 109 ms 12680 KB Partially correct
54 Partially correct 64 ms 14444 KB Partially correct
55 Partially correct 148 ms 24300 KB Partially correct
56 Partially correct 130 ms 18412 KB Partially correct
57 Partially correct 124 ms 16748 KB Partially correct
58 Partially correct 127 ms 15340 KB Partially correct
59 Partially correct 131 ms 13548 KB Partially correct
60 Partially correct 74 ms 12648 KB Partially correct
61 Partially correct 83 ms 12668 KB Partially correct
62 Partially correct 88 ms 12780 KB Partially correct
63 Partially correct 82 ms 12328 KB Partially correct
64 Partially correct 100 ms 13036 KB Partially correct
65 Partially correct 91 ms 13036 KB Partially correct
66 Partially correct 76 ms 12396 KB Partially correct
67 Partially correct 57 ms 10732 KB Partially correct
68 Partially correct 76 ms 11884 KB Partially correct
69 Partially correct 92 ms 13164 KB Partially correct
70 Partially correct 81 ms 12780 KB Partially correct
71 Partially correct 60 ms 10860 KB Partially correct
72 Partially correct 66 ms 11500 KB Partially correct
73 Partially correct 85 ms 13164 KB Partially correct
74 Partially correct 82 ms 12524 KB Partially correct
75 Partially correct 114 ms 13548 KB Partially correct
76 Partially correct 110 ms 13036 KB Partially correct
77 Partially correct 92 ms 13164 KB Partially correct
78 Partially correct 63 ms 10604 KB Partially correct
79 Partially correct 78 ms 11392 KB Partially correct
80 Partially correct 117 ms 13548 KB Partially correct
81 Partially correct 98 ms 13420 KB Partially correct
82 Partially correct 86 ms 12744 KB Partially correct