hdu 5424 Rikka with Graph II 哈密頓通路,hdu5424

來源:互聯網
上載者:User

hdu 5424 Rikka with Graph II 哈密頓通路,hdu5424

Rikka with Graph IITime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 367    Accepted Submission(s): 90


Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has a non-direct graph with n vertices and n edges. Now he wants you to tell him if there exist a Hamiltonian path.

It is too difficult for Rikka. Can you help her? 
InputThere are no more than 100 testcases.

For each testcase, the first line contains a number n(1≤n≤1000).

Then n lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v.
 
OutputFor each testcase, if there exist a Hamiltonian path print "YES" , otherwise print "NO". 
Sample Input
41 11 22 32 431 22 33 1
 
Sample Output
NOYESHintFor the second testcase, One of the path is 1->2->3If you doesn't know what is Hamiltonian path, click here (https://en.wikipedia.org/wiki/Hamiltonian_path).
 
SourceBestCoder Round #53 (div.2) 
Recommendhujie   |   We have carefully selected several similar problems for you:  5426 5425 5422 5421 5420 題意,給出一個n點n邊的圖,要求是否存在哈密頓通路。

哈密頓通路:經過每個點有且僅有一次的一條通路。

由於圖,只有n個邊,那麼最小度的那個邊,最大隻有2,先判定,是否連通,如果連通,每次,都找那個度最小的作為入點,因為,最小度的點,一定會成為某條路的端點,這樣就可以用o(n )的複雜度,找到一條哈密頓迴路。

給個測試資料,

5 

1 5 1 2 2 3 2 4 1 3

#define N 1005#define M 100005#define maxn 205#define MOD 1000000000000000007int n,a,b,d[N],minx,mi;vector<int> p[N];bool vis[N];void DFS(int x){    vis[x] = true;    FI(p[x].size()){        int goal = p[x][i];        if(!vis[goal]){            DFS(goal);        }    }}int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);     while(S(n)!=EOF)    {        FI(n+1) p[i].clear();        fill(d,0);        FI(n){            S2(a,b);            if(a != b){               p[a].push_back(b);               p[b].push_back(a);               d[a]++;d[b]++;            }        }        bool isConnect = true;        fill(vis,false);        DFS(1);        For(i,1,n+1){            if(!vis[i]){                isConnect = false;                break;            }        }        if(!isConnect){            printf("NO\n");            continue;        }        minx = N;mi = 1;        For(i,1,n+1){            if(d[i] < minx){                mi = i;                minx = d[i];            }        }        fill(vis,false);        vis[mi] = true;        while(true){            bool isChange = false;            minx = N;            int mit = -1;            FI(p[mi].size()){                int goal = p[mi][i];                if(!vis[goal]){                    if(d[goal] < minx){                        mit = goal;                        minx = d[goal];                    }                    isChange = true;                }            }            if(!isChange) break;            mi = mit;            vis[mi] = true;        }        isConnect = true;        For(i,1,n+1){            if(!vis[i]){                isConnect = false;                break;            }        }        if(!isConnect)            printf("NO\n");        else            printf("YES\n");    }    //fclose(stdin);    //fclose(stdout);    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.