Sgu-520-Fire in the Country

Source: Internet
Author: User

A undirected connected graph with n vertices and m edges. At the beginning, node 1 caught fire and it took one day for the fire to spread to its adjacent vertex. At the beginning, a robot was also located at Node 1, however, when the robot leaves, node 1 will catch fire. Vladimir and Nikolay take turns to control the robot. The robot can only move to another adjacent point in one day. In the end, whoever has to let the robot go to the fire will lose, output winner.

 

--> In the game in the search, the strategy is to let the fire go and enter the dead end. The opponent will not go any more in the next day (if a sub-state has a first hand, it will be defeated, so now this status is the first hand to win ).

F [I] indicates how many days the fire will take to reach the node I.

 

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 1000 + 10;int n, m, head[maxn], nxt[maxn<<1], u[maxn<<1], v[maxn<<1], ecnt, f[maxn];void init(){    for(int i = 1; i <= n; i++) head[i] = -1;    ecnt = 0;}void addEdge(int uu, int vv){    u[ecnt] = uu;    v[ecnt] = vv;    nxt[ecnt] = head[uu];    head[uu] = ecnt;    ecnt++;}void read(){    int i, uu, vv;    for(i = 0; i < m; i++){        scanf("%d%d", &uu, &vv);        addEdge(uu, vv);        addEdge(vv, uu);    }}void bfs(){    queue<int> qu;    while(!qu.empty()) qu.pop();    f[1] = 1;    qu.push(1);    while(!qu.empty()){        int u = qu.front(); qu.pop();        for(int e = head[u]; e != -1; e = nxt[e]) if(!f[v[e]]){            f[v[e]] = f[u] + 1;            qu.push(v[e]);        }    }}void fire(){    memset(f, 0, sizeof(f));    bfs();}bool dfs(int u){    for(int e = head[u]; e != -1; e = nxt[e]) if(f[v[e]] == f[u] + 1 && !dfs(v[e])) return 1;    return 0;}int main(){    while(scanf("%d%d", &n, &m) == 2){        init();        read();        fire();        if(dfs(1)) printf("Vladimir\n");        else printf("Nikolay\n");    }    return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.