Codeforces Testing Round #5 B DFS
来源:互联网 发布:枕头选购 知乎 编辑:程序博客网 时间:2024/05/29 09:12
题目链接: http://codeforces.com/problemset/problem/267/B
#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <iomanip>#include <set>using namespace std;const int inf = 0x7fffffff;const int maxn = 10000;int x[105],y[105];int fw[10][10];int a[105],b[105];int k[10];int mj,t;void DFS(int x){ for(int i=0;i<=6;++i) if(fw[x][i]>0){ fw[x][i]--; fw[i][x]--; //cout<<i<<"----"<<endl; DFS(i); //cout<<i<<"----"<<endl; t++; a[t]=x, b[t]=i; //缓存结果 }}int main(){ int n; while(cin>>n){ memset(fw,0,sizeof(fw)); for(int i=1;i<=n;++i){ cin>>x[i]>>y[i]; fw[x[i]][y[i]]++; fw[y[i]][x[i]]++; //存在的双向边 k[x[i]]++; k[y[i]]++; //记下数据 } //cout<<fw[5][5]<<endl; mj=x[1],t=0; for(int i=0;i<=6;++i) if(k[i]%2) mj=i,t++; if(t!=2&&t!=0){ //出现的数字为奇数次的数有奇数个,不可能 puts("No solution"); continue; } t=0; DFS(mj); //从出现奇数次的数字开始搜 //cout<<t<< ' ' <<n<<endl; if(t<n){ // 没有把所有的边都用上, 所以不可能 puts("No solution"); continue; } for(int i=t;i>=1;--i){ for(int j=1;j<=n;++j) if(a[i]==x[j]&&b[i]==y[j]){ // 与数据同向 cout<<j<<' '<<'+'<<endl; x[j]=10; // 用过了舍去 break; } else if(a[i]==y[j]&&b[i]==x[j]){ // 与数据反向 cout<<j<<' '<<'-'<<endl; x[j]=10; // 用过了舍去 break; } } } return 0;}
- Codeforces Testing Round #5 B DFS
- Codeforces Testing Round #10 B. Balancer
- Codeforces Testing Round #12 B - Restaurant ( 贪心 )
- Codeforces Testing Round #12 B. Restaurant (贪心)
- Codeforces Testing Round #12 B. Restaurant (贪心)
- Codeforces Testing Round #14 (Unrated) B
- 【Codeforces Testing Round #4】Codeforces 147B Smile House
- Codeforces Round #306 (Div. 2) B(dfs)
- Codeforces Round #306 (Div. 2) B DFS
- Codeforces Round #335 (Div. 2) B. Testing Robots 模拟
- Codeforces Round #335 (Div. 2) 606B Testing Robots(模拟)
- Codeforces Round #335 (Div. 2) B.Testing Robots
- Codeforces Round #335 (Div. 2) B. Testing Robots (模拟)
- Codeforces Round #335 (Div. 2) B. Testing Robots
- Codeforces Testing Round #3
- Codeforces Testing Round #3
- Codeforces Testing Round #9
- 【Codeforces】Testing Round #12
- OpenCV视频播放控制
- 根据前序遍历序列和中序遍历序列构造二叉树算法
- 螺旋队列
- Qt Style Sheets Examples
- easyUI-datagrid 初始化数据为50条的问题
- Codeforces Testing Round #5 B DFS
- 沃信科技ERP系统容灾
- 去除锁屏
- VIM
- hdu 1004
- 单例模式
- 简易加密狗实现
- 黑马程序员--高新反射
- Linux中eventfd函数调用解析