ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1045. Funny Game

Accepted code.
Posted by Jumbo 31 Dec 2011 01:36
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector < vector < int > > g;
vector < bool > d;

void dfs(int v, int p = -1)
{
    if ((int)g[v].size()==1) d[v]=false;
    d[v] = false;
    for (int i = (int)g[v].size()-1; i >= 0; i--)
    {
        int to = g[v][i];
        if (to == p) continue;
        dfs(to,v);
        if (!d[to]) d[v] = true;
    }
}

int main() {
    int n,root;
    scanf("%d%d",&n,&root);
    g.resize(n+1); d.resize(n+1);
    for (int i=1;i<=n-1;i++)
    {
        int u,v; scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i=1;i<=n;i++)
        sort(g[i].begin(),g[i].end());

    dfs(root);
    if (d[root])
    {
        for (int i = 0; i<(int)g[root].size(); i++)
            if (!d[g[root][i]]) { printf("First player wins flying to airport %d",g[root][i]); return 0;}
    }
    puts("First player loses");
    return 0;
}