Luogu p Rui's wooden stick, P1333 wooden stick

Source: Internet
Author: User

Luogu p Rui's wooden stick, P1333 wooden stick
Description

Rui has a bunch of toy wooden sticks, each of which has been infected with a certain color. Now he suddenly has an idea that he wants to splice these wooden sticks together into a line, in addition, the color of the two ends of the contact with the wooden stick is the same. The color of the two ends of each wooden stick is given. Is there any arrangement that meets the requirements.

For example, if there are only two wooden sticks, the colors at both ends are red and blue, and the colors at both ends are red and yellow, blue --- red | red ---- yellow is a suitable arrangement.

Input/Output Format

Input Format:

 

There are several lines in the input. Each line contains two words, indicating the color at both ends of a wooden stick. The word is composed of lowercase letters, and the word length cannot exceed 10 letters. A maximum of 250000 wooden sticks can be entered.

 

Output Format:

 

If the sticks can be arranged as required, output Possible; otherwise, output Impossible

 

Input and Output sample input sample #1: Copy
blue redred violetcyan blueblue magentamagenta cyan
Output example #1: Copy
Possible

Let's look at the dots of the same color as a node.

Then this question is to determine whether there is an Euler loop.

The Euler's loop is basically determined by DFS.

But in fact, you can also query the set.

Set $ x $ to the number of successful merge operations, and $ n $ to the number of points.

Then the whole graph is an Euler loop. if and only when $ x >n-1 and the odd degree period is $0 $ or $2 $

By the way

Pbds is really a good thing.

#include<cstdio>#include<cstring>#include<map>#include<iostream>#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/hash_policy.hpp>using namespace __gnu_pbds;using namespace std;const int MAXN=1e6+10;gp_hash_table<string,int>mp;int tot=0,fa[MAXN],inder[MAXN];int find(int x){    if(fa[x]==x) return fa[x];    else return fa[x]=find(fa[x]);}int unionn(int x,int y){    inder[x]++;inder[y]++;    int fx=find(x),fy=find(y);    if(fx==fy) return 0;    fa[fx]=fy; return 1;}int main(){    ios::sync_with_stdio(false);     for(int i=1;i<=250001;i++) fa[i]=i;    string a,b;    int ans=0;    while(cin>>a>>b)    {        int posa=mp[a]?mp[a]:mp[a]=++tot;        int posb=mp[b]?mp[b]:mp[b]=++tot;        ans+=unionn(posa,posb);        }    if(ans<tot-1) {printf("Impossible\n");return 0;}    int attack=0;    for(int i=1;i<=tot;i++)        if(inder[i]&1) attack++;     if(attack>2) {printf("Impossible\n");return 0;}    printf("Possible\n");    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.