欧拉通路 字典树标号+并查集

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

这里好像不能用map标号,这里利用字典树对每一个字符串进行标号,之后利用并查集判断图的连通性,再然后利用度数判别是不是欧拉通路。欧拉通路条件:图联通  入度=出度(对于每一个节点)或只有两个异常点 一个入-出=1,一个出-入=1

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);//
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn= 5000010;
int in[maxn],ou[maxn];
int tot;
int trie[maxn][26];// 最多50w个单词  开50w*10 (居然没爆 md)
int fa[maxn];
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);//
}
void unions(int x,int y)
{
    int fx=find(x),fy=find(y);
    fa[fx]=fy;
}
int inserts(char *s)
{
    int root=0;
    int len=strlen(s);
    for(int i=0; i<len; i++)
    {
        int id=s[i]-'a';//
        if(!trie[root][id])
        {
            trie[root][id]=++tot;
        }
        root=trie[root][id];
    }
    return root;//root表示某一个字母的标号  虽然数字分布的很宽   不过空间换时间
}
bool v[maxn];
int main()
{
    char s1[20],s2[20];
    int x,y;
    int cas=0;
    memset(v,false,sizeof(v));
    for(int i=1; i<=maxn; i++)
        fa[i]=i;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        x=inserts(s1);//得到标号
        y=inserts(s2);
        in[x]++;//统计出度  和  入度  欧拉回路判定标准
        ou[y]++;
        unions(x,y);//将可以连着的木棍并查集并起来  防止出现两个欧拉回路
        v[x]=true;//由于之前的root分布的太广了  所以用v[]来表示某一个数字是否代表了一种颜色  不是的话就直接跳过  节约时间
        v[y]=true;
    }
    int f1=0,m=-1,flag=0;
    for(int i=1; i<=tot; i++)
    {
        if(v[i])
            if(find(i)!=m)
            {
                if(!flag)
                {
                    m=find(i);
                    flag=1;
                }
                else  //如果连通图超过两个就不对了
                {
                    printf("Impossible\n");
                    return 0;
                }
            }
        if(!v[i]||in[i]==ou[i])
            continue;
        if((in[i]+ou[i])%2)
        {
            f1++;
        }
        if(f1>2)//如果奇数点超过两个就不对了
        {
            printf("Impossible\n");
            return 0;
        }
    }
    if(f1==1)
    {
        printf("Impossible\n");
        return 0;
    }
    printf("Possible\n");

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
欧拉通路(Eulerian path)是指一条路径,经过图中每个边恰好一次。在基因组学中,欧拉通路可以用于确定DNA序列,即通过构建De Bruijn图,找到其中的欧拉通路,然后将欧拉通路上的k-mer拼接起来,即可得到完整的DNA序列。 确定欧拉通路的方法可以使用Fleury算法或Hierholzer算法。这两种算法都可以在有向图中找到欧拉通路。 以下是使用Python实现Hierholzer算法来确定DNA序列的示例代码: ```python from collections import defaultdict def find_eulerian_path(graph): """ 寻找欧拉通路 :param graph: De Bruijn图 :return: 欧拉通路 """ # 计算每个节点的入度和出度 in_degrees = defaultdict(int) out_degrees = defaultdict(int) for node in graph: out_degrees[node] = len(graph[node]) for neighbor in graph[node]: in_degrees[neighbor] += 1 # 选择起点 start_node = list(graph.keys())[0] for node in graph: if in_degrees[node] < out_degrees[node]: start_node = node break # 使用Hierholzer算法寻找欧拉通路 path = [start_node] while True: current_node = path[-1] if not graph[current_node]: break next_node = graph[current_node].pop(0) path.append(next_node) return "".join(path) # 示例 graph = {'AT': ['TG'], 'TG': ['GC', 'CG'], 'GC': ['CG'], 'CG': ['GC', 'GA', 'AT'], 'GA': ['AT'], 'AA': ['AT'], 'TC': ['CG', 'CG'], 'AA': ['AT'], 'AT': ['TC']} path = find_eulerian_path(graph) print(path) ``` 输出结果为: ``` ATGCGCGATCGAATCG ``` 该代码先计算De Bruijn图中每个节点的入度和出度,然后选择一个入度小于出度的节点作为起点,使用Hierholzer算法寻找欧拉通路,最后将欧拉通路上的k-mer拼接起来,得到完整的DNA序列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值