hdu 1116 并查集和欧拉路径

---恢复内容开始---

把它看成是一个图

只是需要欧拉路径就可以了 首尾能连成一条线即可

如果要判断这个图是否连通 得用并查集

在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能否循环

只需要在union的时候做做调整 让比较大的父亲节点的父亲节点等于小的父亲节点 向1靠拢就可以

但是在这里面 是向出现过的最小的字母的排序靠拢 所以要记录

而且for循环26个字母的时候 只对出现过的字母做判断它是否与最小的字母可以连通

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
int ru[30];
int chu[30];
int fa[30];
int vis[30];
void init()
{
    for(int i=1;i<=26;i++)
    {
        ru[i]=0;
        chu[i]=0;
        fa[i]=i;
        vis[i]=0;
    }
}
int find(int i)
{
    return fa[i]==i?i:find(fa[i]);
}
void un(int a,int b)
{
    int aa=find(a);
    int bb=find(b);
    if(aa>bb)
        fa[aa]=bb;
    else fa[bb]=aa;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        char s[2000];
        init();
        int minn=26;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int a=s[0]-‘a‘+1;
            int len=strlen(s);
            int b=s[len-1]-‘a‘+1;
            chu[a]++;
            ru[b]++;
            un(a,b);
            vis[a]++;
            vis[b]++;
            if(a<minn)
                minn=a;
            if(b<minn)
                minn=b;
        }
        int yi=0;
        int er=0;
        int san=0;
        bool ok=true;
        for(int i=1;i<=26;i++)
        {
            if(ru[i]==chu[i])
                yi++;
            else if(ru[i]==chu[i]+1)
                er++;
            else if(ru[i]==chu[i]-1)
                san++;
            if(vis[i]!=0)
            {
                if(find(i)!=minn)
                    ok=false;
            }
        }
        if(yi==24&&er==1&&san==1&&ok==true)
            printf("Ordering is possible.\n");
        else if(yi==26&&ok==true)
            printf("Ordering is possible.\n");
        else printf("The door cannot be opened.\n");
    }
}

  

---恢复内容结束---

时间: 2024-10-25 07:36:45

hdu 1116 并查集和欧拉路径的相关文章

hdu 1116(并查集+欧拉路径)

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7080    Accepted Submission(s): 2398 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

hdu 1116 Play on Words 欧拉路径+并查集

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7791    Accepted Submission(s): 2676 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路.统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求.还有别忘了判断是否连通,此时用到并查集,图中所有的边

Play on Words HDU - 1116 (并查集 + 欧拉通路)

Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys