답안 #589784

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589784 2022-07-05T09:15:27 Z 박상훈(#8406) 갈라파고스 여행 (FXCUP3_island) C++17
0 / 100
19 ms 35576 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[300300], G[300300], ans, P[300300];
vector<pair<int, int>> E;
int D;
map<int, int> mp[300300];

void dfs(int s, int pa = -1, int dep = 1){
    D = max(D, dep);
    P[dep].push_back(s);
    for (auto &v:adj[s]) if (v!=pa){
        G[s].push_back(v);
        dfs(v, s, dep+1);
    }
}

mt19937 seed(1234658);
int add(int s){
    shuffle(E.begin(), E.end(), seed);
    for (auto iter=E.begin();iter!=E.end();iter++) if (s!=iter->first){
        if (mp[s].find(iter->first)==mp[s].end()){
            ans.push_back(iter->first);
            ans.push_back(iter->second);
            int ret = iter->second;
            E.erase(iter);
            return ret;
        }
    }
    return 0;
}

int next_level(int d, int s){
    vector<pair<int, int>> nE;
    for (auto &x:P[d]) if (!G[x].empty()){

        ans.push_back(x);
        ans.push_back(G[x][0]);
        for (int i=1;i<(int)G[x].size();i++) nE.emplace_back(G[x][i], x);
        s = G[x][0];

        while(true){
            int nxt = add(s);
            if (!nxt) break;
            s = nxt;
        }
    }
    for (auto &p:nE) E.push_back(p);
    return s;
}

void construct(int R){
    int s = R;
    ans.push_back(s);
    for (int i=D-1;i>2;i--){
        s = next_level(i, s);
    }
    ans.push_back(P[2][0]);
    ans.push_back(R);
}

bool chk(const vector<int> &ans){
    for (int i=0;i<(int)ans.size()-1;i++){
        int tmp = mp[ans[i]][ans[i+1]];
        if (tmp==-1 || ans[i]==ans[i+1]) return 0;
        if (tmp!=i%2) return 0;
        mp[ans[i]][ans[i+1]] = -1, mp[ans[i+1]][ans[i]] = -1;
    }
    return 1;
}

int main(){
    int n;
    scanf("%d", &n);
    for (int i=1;i<=n-1;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);

        mp[x][y] = 1;
        mp[y][x] = 1;
    }

    int rt = 0;
    for (int i=1;i<=n;i++) if (adj[i].size()==1) {rt = i; break;}

    dfs(rt);
    if (D<4) {printf("0\n"); return 0;}
    construct(rt);

    printf("%d\n", (int)ans.size()-1);
    for (auto &x:ans) printf("%d ", x);

    assert(chk(ans));
    return 0;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
island.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Correct
2 Correct 16 ms 35540 KB Correct
3 Incorrect 16 ms 35576 KB Wrong
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 35540 KB Correct
2 Correct 16 ms 35540 KB Correct
3 Incorrect 16 ms 35576 KB Wrong
4 Halted 0 ms 0 KB -