hdu 4337 King Arthur's Knights(dfs)
2013-08-07 23:10
453 查看
Problem Description
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.
Input
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.
Output
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".
Sample Input
3 3
1 2
2 3
1 3
4 4
1 4
2 4
2 3
1 3
Sample Output
1 2 3
1 4 2 3
题目大意:n个人坐在一个圆桌旁,输入m对有亲密关系得人,要求每个人相邻的都是跟他亲密的,如果存在输出任意一种情况(这里题目大大简化了),否则输出no solution
AC代码:
#include<stdio.h>
#include<string.h>
int dir[155][155],vis[155],out[155];
int n,m;
bool dfs(int x,int dep)
{
int i;
vis[x]=1;
out[dep]=x;
if(dep==n)
{
if(dir[1][x]) return true;
else{ vis[x]=0;return false;}
}
for(i=1;i<=n;i++)
{
if(dir[x][i]&&!vis[i])
if(dfs(i,dep+1))
return true;
}
vis[x]=0;
return false;
}
int main()
{
int i,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dir,0,sizeof(dir));
memset(vis,0,sizeof(vis));
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
dir[x][y]=dir[y][x]=1;
}
if(!dfs(1,1))
{
printf("no solution\n");
continue;
}
for(i=1;i<n;i++)
printf("%d ",out[i]);
printf("%d\n",out
);
}
return 0;
}
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.
Input
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.
Output
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".
Sample Input
3 3
1 2
2 3
1 3
4 4
1 4
2 4
2 3
1 3
Sample Output
1 2 3
1 4 2 3
题目大意:n个人坐在一个圆桌旁,输入m对有亲密关系得人,要求每个人相邻的都是跟他亲密的,如果存在输出任意一种情况(这里题目大大简化了),否则输出no solution
AC代码:
#include<stdio.h>
#include<string.h>
int dir[155][155],vis[155],out[155];
int n,m;
bool dfs(int x,int dep)
{
int i;
vis[x]=1;
out[dep]=x;
if(dep==n)
{
if(dir[1][x]) return true;
else{ vis[x]=0;return false;}
}
for(i=1;i<=n;i++)
{
if(dir[x][i]&&!vis[i])
if(dfs(i,dep+1))
return true;
}
vis[x]=0;
return false;
}
int main()
{
int i,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dir,0,sizeof(dir));
memset(vis,0,sizeof(vis));
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
dir[x][y]=dir[y][x]=1;
}
if(!dfs(1,1))
{
printf("no solution\n");
continue;
}
for(i=1;i<n;i++)
printf("%d ",out[i]);
printf("%d\n",out
);
}
return 0;
}
相关文章推荐
- HDU 4337 King Arthur's Knights(暴力dfs)
- King Arthur's Knights hdu 4337 汉密尔顿图(模板)
- hdu - 4337 - King Arthur's Knights - 哈密顿回路
- HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
- hdu4337 King Arthur's Knights(dfs回溯)
- HDU&nbsp;4337&nbsp;King&nbsp;Arthur&#039;s&nbsp;Knights(…
- hdu 4337 King Arthur's Knights
- HDU 4337 King Arthur's Knights——回溯法
- hdu4337 King Arthur's Knights
- HDU 4337 King Arthur's Knights (图论+dfs) = =
- hdu4337 King Arthur's Knights
- HDU4337 King Arthur's Knights【哈密顿回路】
- HDU 1198(并查集;dfs)
- HDU_2196_Computer(树上节点的最长路径 · dfs / bfs)
- HDU 5927 Auxiliary Set(dfs+逆向思维)
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- HDU 4274 Spy's Work (dfs)
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5676 dfs打表
- (step4.3.4)hdu 1258(Sum It Up——DFS)