hdu 1116 Play on Words

简介: 点击打开链接hdu1116 思路: 欧拉回路 分析: 1 题目给定n个字符串,判断是否可以按照题目的要求可以组成一个长串 2 很明显题目是要求有向图是否有欧拉回路或者是欧拉道路 3 首先我们先判断有向图是否是单连通,那么这里面我们可以...

点击打开链接hdu1116

思路: 欧拉回路
分析:
1 题目给定n个字符串,判断是否可以按照题目的要求可以组成一个长串
2 很明显题目是要求有向图是否有欧拉回路或者是欧拉道路
3 首先我们先判断有向图是否是单连通,那么这里面我们可以用并查集去求单连通性
4 如果是单连通的话,我们知道对于有向图来说如果存在欧拉道路的话那么最多有两个点的入度不等于长度并且入度和出度的绝对值为1,其它所有点的入度等于出度
5 注意n=1的时候都认为是可以满足条件

代码:

#include<set>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 30;
int n , inDegree[N] , outDegree[N];
int mat[N][N] , father[N];
bool vis[N];

void init(){
    memset(vis , false , sizeof(vis));
    memset(inDegree , 0 , sizeof(inDegree));
    memset(outDegree , 0 , sizeof(outDegree));
    memset(mat , 0 , sizeof(mat));
    for(int i = 0 ; i < N ; i++)
        father[i] = i;
}

int find(int x){
    if(father[x] != x)
       father[x] = find(father[x]);
    return father[x];
}

bool isOk(){
    set<int>s;
    for(int i = 0 ; i < N ; i++)
       if(vis[i])
           s.insert(find(i));
    if(s.size() > 1)//如果有多个连通分支即不满足
       return false;
    int cnt = 0;
    for(int i = 0 ; i < N ; i++){
       if(abs(inDegree[i]-outDegree[i]) > 1) //如果绝对值大于1则不满足
          return false;
       if(abs(inDegree[i]-outDegree[i]) == 1) 
          cnt++;
    }
    return cnt <= 2;//最多2个点的出度和入度绝对值为1
}

int main(){
    int Case , x , y;
    char str[1010];
    scanf("%d" , &Case);
    while(Case--){
         scanf("%d" , &n); 
         init();
         for(int i = 0 ; i < n ; i++){
             scanf("%s" , str);  
             x = str[0]-'a';
             y = str[strlen(str)-1]-'a';
             vis[x] = vis[y] = true;
             inDegree[y]++;
             outDegree[x]++;
             mat[x][y]++;
             father[find(x)] = find(y);
         }
         if(n == 1 || isOk()) 
             puts("Ordering is possible.");
         else
             puts("The door cannot be opened.");
    }
    return 0;
}



目录
相关文章
|
6月前
codeforces 302 B. Eugeny and Play List
题目链接 有n首歌,编号从1到n,每首歌播放时间为t,播放次数为c,n首歌按次序播放,有m个询问,输出第v分钟正在播放的歌曲编号。 很简单的二分查找,直接贴代码。
22 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 索引
LeetCode 55. Jump Game
给定一个非负整数数组,您最初定位在数组的第一个索引处。 数组中的每个元素表示该位置的最大跳转长度。 确定您是否能够到达最后一个索引。
71 0
LeetCode 55. Jump Game
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
71 0
|
人工智能 算法 大数据
LeetCode - 45. Jump Game II
45. Jump Game II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组a,玩家的初始位置在idx=0,玩家需要到达的位置是idx=a.
918 0
|
C++ Python
[LeetCode] Jump Game II
This problem has a nice BFS structure. Let's illustrate it using the example nums = [2, 3, 1, 1, 4] in the problem statement.
885 0
[LeetCode] Jump Game
This problem has a very concise solution in this link, just 4-lines. The code is rewritten below. 1 class Solution { 2 public: 3 bool canJump(vector& nums) { 4 int i = 0, n = nums.
741 0