답안 #343024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343024 2021-01-03T11:15:53 Z urd05 Village (BOI20_village) C++14
31 / 100
116 ms 22508 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[1000];
bool gone[1000];
vector<int> v;
int dist[1000];

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("%d\n",ret);
    for(int i=0;i<n;i++) {
        nxt2[v[i]]=v[(i+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: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);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 4 ms 5484 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 4 ms 5484 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 5 ms 5484 KB Output is correct
7 Correct 3 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 3 ms 5484 KB Output is correct
10 Correct 3 ms 5484 KB Output is correct
11 Correct 3 ms 5484 KB Output is correct
12 Correct 3 ms 5484 KB Output is correct
13 Correct 3 ms 5484 KB Output is correct
14 Correct 3 ms 5484 KB Output is correct
15 Correct 3 ms 5484 KB Output is correct
16 Correct 3 ms 5484 KB Output is correct
17 Correct 4 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5484 KB Output is correct
2 Correct 9 ms 5484 KB Output is correct
3 Correct 8 ms 5484 KB Output is correct
4 Partially correct 20 ms 5484 KB Partially correct
5 Partially correct 21 ms 5484 KB Partially correct
6 Correct 18 ms 5484 KB Output is correct
7 Correct 20 ms 5612 KB Output is correct
8 Correct 23 ms 5484 KB Output is correct
9 Correct 21 ms 5484 KB Output is correct
10 Correct 19 ms 5484 KB Output is correct
11 Correct 17 ms 5484 KB Output is correct
12 Correct 14 ms 5484 KB Output is correct
13 Correct 14 ms 5484 KB Output is correct
14 Correct 16 ms 5484 KB Output is correct
15 Correct 16 ms 5484 KB Output is correct
16 Correct 21 ms 5484 KB Output is correct
17 Correct 20 ms 5484 KB Output is correct
18 Correct 16 ms 5484 KB Output is correct
19 Correct 18 ms 5484 KB Output is correct
20 Correct 19 ms 5504 KB Output is correct
21 Correct 16 ms 5484 KB Output is correct
22 Correct 16 ms 5484 KB Output is correct
23 Correct 18 ms 5484 KB Output is correct
24 Partially correct 16 ms 5484 KB Partially correct
25 Correct 8 ms 5504 KB Output is correct
26 Correct 20 ms 5612 KB Output is correct
27 Correct 8 ms 5484 KB Output is correct
28 Correct 18 ms 5484 KB Output is correct
29 Correct 16 ms 5484 KB Output is correct
30 Correct 15 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 4 ms 5484 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 4 ms 5484 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 5 ms 5484 KB Output is correct
7 Correct 3 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 3 ms 5484 KB Output is correct
10 Correct 3 ms 5484 KB Output is correct
11 Correct 3 ms 5484 KB Output is correct
12 Correct 3 ms 5484 KB Output is correct
13 Correct 3 ms 5484 KB Output is correct
14 Correct 3 ms 5484 KB Output is correct
15 Correct 3 ms 5484 KB Output is correct
16 Correct 3 ms 5484 KB Output is correct
17 Correct 4 ms 5484 KB Output is correct
18 Correct 5 ms 5484 KB Output is correct
19 Correct 9 ms 5484 KB Output is correct
20 Correct 8 ms 5484 KB Output is correct
21 Partially correct 20 ms 5484 KB Partially correct
22 Partially correct 21 ms 5484 KB Partially correct
23 Correct 18 ms 5484 KB Output is correct
24 Correct 20 ms 5612 KB Output is correct
25 Correct 23 ms 5484 KB Output is correct
26 Correct 21 ms 5484 KB Output is correct
27 Correct 19 ms 5484 KB Output is correct
28 Correct 17 ms 5484 KB Output is correct
29 Correct 14 ms 5484 KB Output is correct
30 Correct 14 ms 5484 KB Output is correct
31 Correct 16 ms 5484 KB Output is correct
32 Correct 16 ms 5484 KB Output is correct
33 Correct 21 ms 5484 KB Output is correct
34 Correct 20 ms 5484 KB Output is correct
35 Correct 16 ms 5484 KB Output is correct
36 Correct 18 ms 5484 KB Output is correct
37 Correct 19 ms 5504 KB Output is correct
38 Correct 16 ms 5484 KB Output is correct
39 Correct 16 ms 5484 KB Output is correct
40 Correct 18 ms 5484 KB Output is correct
41 Partially correct 16 ms 5484 KB Partially correct
42 Correct 8 ms 5504 KB Output is correct
43 Correct 20 ms 5612 KB Output is correct
44 Correct 8 ms 5484 KB Output is correct
45 Correct 18 ms 5484 KB Output is correct
46 Correct 16 ms 5484 KB Output is correct
47 Correct 15 ms 5484 KB Output is correct
48 Runtime error 116 ms 22508 KB Execution killed with signal 11 (could be triggered by violating memory limits)
49 Halted 0 ms 0 KB -