无序字母对
https://www.luogu.org/problemnew/show/P1341
題解:
參考文章:https://blog.csdn.net/weixin_43272781/article/details/88553573
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int using namespace std; typedef long long ll; //typedef __int128 lll; const int N=128+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,q; int ans,cnt,flag,temp,sum; bool a[N][N]; bool vis[N]; int deg[N]; char str1,str2; char ANS[N]; void dfs(int rt){for(int i='A';i<='z';i++){if(a[rt][i]){a[rt][i]=a[i][rt]=0;dfs(i);}}ANS[n-ans++]=rt; }int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//while(t--){scanf("%d",&n);for(int i=1;i<=n;i++){cin>>str1>>str2;a[str1][str2]=a[str2][str1]=1;deg[str1]++;deg[str2]++;}int h=0;for(int i='A';i<='z';i++){if(deg[i]&1){cnt++;if(!h)h=i;}}if(!h)for(int i='A';i<='z';i++){if(deg[i]){h=i;break;}}if(cnt&&cnt!=2){cout<<"No Solution";return 0;}dfs(h);if(ans<n+1){cout<<"No Solution";return 0;}ANS[n+1]='\0';cout<<ANS<<endl;//}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結(jié)
- 上一篇: 欧拉路径(Euler_Path)和欧拉回
- 下一篇: 强连通分量(Strongly_Conne