CF1255C League of Leesins (graph theory)

Meaning of the questions:

Gives N-2 triplets, internal sequence triplets which have been disrupted, while the sequence between the different triplets are also upset, reducing the sequence you!

answer:

Map building, inside each triple build two group side, then count the number of occurrences of each element in all groups.

Not difficult to find, there have been only once must be the start or end.

After determining the starting point, the start point of the traverse connected, appears twice is the second point.

After determining the two points before the point of time from the first point where to find the connected node is not accessible, it is determined third node, and then push back the first two points, the loop processing.

Thinking + graph theory, do not have to deal with ...

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
vector<int> g[maxn];
map<int,int> pos;
int visit[maxn];
int main () {
    int N;
    scanf("%d",&N);
    for (int i=1;i<=N-2;i++) {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        g[x].push_back(y),g[x].push_back(z);
        g[y].push_back(x),g[y].push_back(z);
        g[z].push_back(y),g[z].push_back(x);
        pos[x]++,pos[y]++,pos[z]++;
    }
    int f1;
    for (f1=1;f1<=N;f1++) 
        if (pos[f1]==1) break;
    int f2;
    if (pos[g[f1][0]]==2) 
        f2=g[f1][0];
    else 
        f2=g[f1][1];
    visit[f1]=1;
    visit[f2]=1;
    printf("%d %d",f1,f2);
    for (int i=1;i<=N-2;i++) {
        int f3;
        for (int j=0;j<g[f1].size();j++) 
            if (!visit[g[f1][j]]) {
                f3=g[f1][j];
                break;
            }
        visit[f3]=1;
        printf(" %d",f3);
        f1=f2,f2=f3;
    }
}

 

Guess you like

Origin www.cnblogs.com/zhanglichen/p/12636470.html