【圖論05】並查集 1001 Play on Words

來源:互聯網
上載者:User

題意:將n個單詞首尾相連組成一個單詞鏈,如:acm->malform->mouse(a -> m -> m -> m ->m -> e)
   並查集+歐拉路實現
      1.並查集判連通,這點不用多說
      2.歐拉路,由圖中可知除二端點外,其餘字母的入度和出度均相等,二端點的出度和入度相差1,還有一種可能是,整個圖就是一個歐拉迴路,此時每個端點的入度和出度均相等

//模板開始#include <string>   #include <vector>   #include <algorithm>   #include <iostream>   #include <sstream>   #include <fstream>   #include <map>   #include <set>   #include <cstdio>   #include <cmath>   #include <cstdlib>   #include <ctime>#include<iomanip>#include<string.h>#define SZ(x) (int(x.size()))using namespace std;int toInt(string s){istringstream sin(s); int t; sin>>t; return t;}template<class T> string toString(T x){ostringstream sout; sout<<x; return sout.str();}typedef long long int64;int64 toInt64(string s){istringstream sin(s); int64 t; sin>>t;return t;}template<class T> T gcd(T a, T b){ if(a<0) return gcd(-a, b);if(b<0) return gcd(a, -b);return (b == 0)? a : gcd(b, a % b);}//模板結束(通用部分)#define ifs cinint findset(int x, int pa[]){return pa[x] != x ? pa[x] = findset(pa[x], pa) : x;}//【圖論05】並查集 1001 Play on Words#define MAX_SIZE 30int next_node[MAX_SIZE];//儲存有向圖的邊int in[MAX_SIZE];//儲存節點的入度int out[MAX_SIZE];//儲存節點的出度int flag[MAX_SIZE];//標記節點是否存在void init()//初始化{for(int i = 0; i < 26; i++){next_node[i] = i;}memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));memset(flag, 0, sizeof(flag));}int findset(int a)//找元素所在集合的代表元(因為用了路徑壓縮,路徑壓縮的主要目的是為了儘快的確定元素所在的集合){while(next_node[a] != a){a = next_node[a];}return a;}void union_nodes(int a, int b)//集合合并{int a1 = findset(a);int b1 = findset(b);next_node[a1] = b1;}int main(){//ifstream ifs("shuju.txt", ios::in);int m, n;ifs>>m;for(int i = 0; i < m; i++){init();ifs>>n;for(int j = 0; j < n; j++)//輸入資料,建立有向圖,併合並相關集合{char data[1005];ifs>>data;int a = data[0] - 'a';int b = data[strlen(data) - 1] - 'a';union_nodes(a, b);out[a]++;in[b]++;flag[a]++;flag[b]++;}int count = 0;for(int j = 0; j < 26; j++)//計算有向圖中連通分支的個數{if(next_node[j] == j && flag[j] != 0){count++;}}if(count >= 2)//當連通分支大於2{cout<<"The door cannot be opened."<<endl;continue;}int f1 = 1;if(count == 0)//當構成迴路{for(int j = 0; j < 26; j++){if(flag[j] == 0){continue;}if(in[j] != out[j]){cout<<"The door cannot be opened."<<endl;f1 = 0;break;}}if(f1 == 1){cout<<"Ordering is possible."<<endl;continue;}else if(f1 == 0){continue;}}int jishu1 = 1;int jishu2 = 1;int f2 = 1;if(count == 1)//當存在一個不是迴路的連通分量{for(int j = 0; j < 26; j++){if(in[j] == out[j]){continue;}else if(in[j] - out[j] == 1){jishu1--;}else if(out[j] - in[j] == 1){jishu2--;}else{cout<<"The door cannot be opened."<<endl;f2 = 0;break;}}if(f2 == 1){cout<<"Ordering is possible."<<endl;continue;}else if(f2 == 0){continue;}}}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.