HDU 4337 King Arthur's Knights (图论+dfs) = =
2013-08-08 12:44
267 查看
King Arthur's Knights
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1518 Accepted Submission(s): 645
Special Judge
[align=left]Problem Description[/align]
I am the bone of my sword. Steel is my body, and the fire is my blood.
- from Fate / Stay Night
You must have known the legend of King Arthur and his knights of the round table. The round table has no head, implying that everyone has equal status. Some knights are close friends with each other, so they prefer to sit next to each other.
Given the relationship of these knights, the King Arthur request you to find an arrangement such that, for every knight, his two adjacent knights are both his close friends. And you should note that because the knights are very united, everyone has at least
half of the group as his close friends. More specifically speaking, if there are N knights in total, every knight has at least (N + 1) / 2 other knights as his close friends.
[align=left]Input[/align]
The first line of each test case contains two integers N (3 <= N <= 150) and M, indicating that there are N knights and M relationships in total. Then M lines followed, each of which contains two integers ai and bi (1 <= ai, bi <=
n, ai != bi), indicating that knight ai and knight bi are close friends.
[align=left]Output[/align]
For each test case, output one line containing N integers X1, X2, ..., XN separated by spaces, which indicating an round table arrangement. Please note that XN and X1 are also considered adjacent. The answer may be not unique, and
any correct answer will be OK. If there is no solution exists, just output "no solution".
[align=left]Sample Input[/align]
3 3 1 2 2 3 1 3 4 4 1 4 2 4 2 3 1 3
[align=left]Sample Output[/align]
1 2 3 1 4 2 3
[align=left]Source[/align]
2012 Multi-University Training Contest 4
[align=left]Recommend[/align]
zhoujiaqi2010
题意:一个圆桌,安排座位。每个人的左右都要是自己的朋友。已知每个人都有一半以上的人是他的朋友。这个题应该用哈密顿回路做的,但是那个前提使得不
可能无解,所以带回溯的深搜可以过,这样就变成和经典素数环一样的了。
感想:
1、首先建图是无向图啊,同学!!!开始就是这里没注意啊喂。要re[a][b]=true,re[b][a]=true;
2、你居然漏打!=EOF,不超时才怪。。。。难怪比完赛下来搜解题报告除了下标起始位置不同,几乎一摸一样你的过不了啊。。。。
代码:
#include<iostream> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; bool re[155][155]; int ok[155]; bool vis[155]; int n,m; int flag; void dfs(int a,int pos) { if(flag==1) return; if(pos==n+1) { if(re[ok[1]][ok ]) { for(int i=1;i<=n;i++) printf("%d%c",ok[i],i==n?'\n':' '); flag=1; } } else { for(int i=2;i<=n;i++) { if(re[a][i]&&!vis[i]) { vis[i]=true; ok[pos]=i; //printf("test: %d %d\n",pos,i); dfs(i,pos+1); //if(flag==1) //break; vis[i]=false; } } } } int main() { int a,b; while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); memset(re,0,sizeof(re)); memset(ok,0,sizeof(ok)); flag=0; for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); re[a][b]=true; re[b][a]=true; } ok[1]=1; dfs(1,2); if(!flag) printf("no solution\n"); } return 0; }
8870377 | 2013-08-08 10:41:14 | Accepted | 4337 | 31MS | 252K | 1272 B | C++ |
#include<iostream> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; bool re[155][155]; int ok[155]; bool vis[155]; int n,m; int flag; void dfs(int a,int pos) { if(flag==1) return; if(pos==n+1) { if(re[ok[1]][ok ]) flag=1; //return; } else { for(int i=2;i<=n;i++) { if(re[a][i]&&!vis[i]&&flag==0) { vis[i]=true; ok[pos]=i; //printf("test: %d %d\n",pos,i); dfs(i,pos+1); //if(flag==1) //break; vis[i]=false; } } } } int main() { int a,b; while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); memset(re,0,sizeof(re)); memset(ok,0,sizeof(ok)); flag=0; for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); re[a][b]=true; re[b][a]=true; } ok[1]=1; dfs(1,2); if(flag) { for(int i=1;i<=n;i++) printf("%d%c",ok[i],i==n?'\n':' '); } else printf("no solution\n"); } return 0; }
8872033 | 2013-08-08 13:20:07 | Accepted | 4337 | 31MS | 252K | 1198 B | C++ |
相关文章推荐
- HDU 4337 King Arthur's Knights(暴力dfs)
- hdu 4337 King Arthur's Knights(dfs)
- 图论 DFS HDU 1241
- HDU2489 Minimal Ratio Tree 解题报告【图论】【Kruskal】【dfs】
- hdu 5348 MZL's endless loop(dfs+图论)
- hdu 5438 Ponds 2015长春网络赛 图论 dfs bfs
- 图论 DFS HDU 1010
- [HDU 4118]Holiday's Accommodation[图论][非递归dfs]
- 图论 DFS HDU 1312
- hdu 5113 Black And White (dfs+强力剪枝)
- HDU1312 Red and Black(DFS)
- hdu 1015 (DFS,暴力)
- HDU 1426 Sudoku Killer DFS
- hdu 1426(Sudoku Killer)DFS
- hdu 1198 dfs(还是水坑变型题)农田灌溉
- HDU1241 Oil Deposits 【DFS】
- hdu 2782 DFS
- HDU 2181 DFS
- HDU 1983 BFS + DFS
- hdu_1536_S-Nim(DFS_SG博弈)