hdu4337 King Arthur's Knights(dfs回溯)

King Arthur's Knights

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1513    Accepted Submission(s): 641
Special Judge


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
 

Source

唉。。

脑残经常有,昨天特别多。很简单的一个dfs竟然从晚上搞到半夜。。。总算是解决了

题目分析:n个点m个关系,求一个序列,每个点与相邻2个点都有关系,首尾2个点算相邻的。

题目分析:因为每个点都至少与一半以上的人有关系,所以此题明显有解,然后就是简单的dfs输出路径就可以了。

比赛的时候脑一残将回溯的标记放在回溯里面,导致一直TLE,后来男神随便写了一个邻接表不带回溯的交了就过了。。。

比赛完又研究了一下此题。

先上一个正确的代码(回溯):

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 170;
const int M = 100005;
int path[N];
int head[N];
bool fuck[N][N];
bool flag[N];
int table[N][N + 1];
struct node
{
    int to,next;
}lcm[M];
int num;
int n,m;
bool ok;

void build(int s,int e)
{
    lcm[num].to = e;
    lcm[num].next = head[s];
    head[s] = num ++;
}

void dfs(int cur,int dp)
{
    if(ok)
        return;
    path[dp] = cur;
    if(dp == n)
    {
        if(fuck[path[1]][path[n]] == true)
        {
            int ii;
            for(ii = 1;ii < n;ii ++)
                printf("%d ",path[ii]);
            printf("%d\n",path[ii]);
            ok = true;
        }
        return;
    }
    int i;
    for(i = head[cur];i != -1;i = lcm[i].next)
    {
        if(flag[lcm[i].to] == false)
        {
            flag[lcm[i].to] = true;
            dfs(lcm[i].to,dp + 1);
            if(ok)
                return;
            flag[lcm[i].to] = false;
        }
    }
}

int main()
{
    int i,a,b;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        num = 0;
        memset(head,-1,sizeof(head));
        memset(fuck,false,sizeof(fuck));
        memset(flag,false,sizeof(flag));
        while(m --)
        {
            scanf("%d%d",&a,&b);
            if(fuck[a][b] == false)
            {
                build(a,b);
                build(b,a);
                fuck[a][b] = fuck[b][a] = true;
            }
        }
        ok = false;
        flag[1] = true;
        dfs(1,1);
    }
    return 0;
}

再贴2个错误版本,予以警示:

版本一:回溯标记在dfs里面:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 170;
const int M = 100005;
int path[N];
int head[N];
bool fuck[N][N];
bool flag[N];
int table[N][N + 1];
struct node
{
    int to,next;
}lcm[M];
int num;
int n,m;
bool ok;

void build(int s,int e)
{
    lcm[num].to = e;
    lcm[num].next = head[s];
    head[s] = num ++;
}

void dfs(int cur,int dp)
{
    if(ok)
        return;
    path[dp] = cur;
    flag[cur] = true;
    if(dp == n)
    {
        flag[cur] = false;//这里!!!少了就TLE了。。。
        if(fuck[path[1]][path[n]] == true)
        {
            int ii;
            for(ii = 1;ii < n;ii ++)
                printf("%d ",path[ii]);
            printf("%d\n",path[ii]);
            ok = true;
        }
        return;
    }
    int i;
    for(i = head[cur];i != -1;i = lcm[i].next)//for(i = 1;i <= table[cur][0];i ++)
    {
        if(flag[lcm[i].to] == false)
        {
            dfs(lcm[i].to,dp + 1);
            if(ok)
                return;
        }
    }
    flag[cur] = false;
}

int main()
{
    int i,a,b;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        num = 0;
        memset(head,-1,sizeof(head));
        memset(fuck,false,sizeof(fuck));
        memset(flag,false,sizeof(flag));
        while(m --)
        {
            scanf("%d%d",&a,&b);
            if(fuck[a][b] == false)
            {
                build(a,b);
                build(b,a);
                fuck[a][b] = fuck[b][a] = true;
            }
        }
        ok = false;
        dfs(1,1);
    }
    return 0;
}

本来回溯标记放里面外面都一样,但是因为第n层有个return,所以return之前一定要先把标记擦掉。。。

错误版本二:非回溯版:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 170;
const int M = 100005;
int path[N];
int head[N];
bool fuck[N][N];
bool flag[N];
int table[N][N + 1];
struct node
{
    int to,next;
}lcm[M];
int num;
int n,m;
bool ok;

void build(int s,int e)
{
    lcm[num].to = e;
    lcm[num].next = head[s];
    head[s] = num ++;
}

void dfs(int cur,int dp)
{
    if(ok)
        return;
    path[dp] = cur;
    if(dp == n)
    {
        if(fuck[path[1]][path[n]] == true)
        {
            int ii;
            for(ii = 1;ii < n;ii ++)
                printf("%d ",path[ii]);
            printf("%d\n",path[ii]);
            ok = true;
        }
        return;
    }
    int i;
    for(i = head[cur];i != -1;i = lcm[i].next)
    {
        if(flag[lcm[i].to] == false)
        {
            flag[lcm[i].to] = true;
            dfs(lcm[i].to,dp + 1);
            if(ok)
                return;
        }
    }
}

int main()
{
    int i,a,b;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        num = 0;
        memset(head,-1,sizeof(head));
        memset(fuck,false,sizeof(fuck));
        memset(flag,false,sizeof(flag));
        while(m --)
        {
            scanf("%d%d",&a,&b);
            if(fuck[a][b] == false)
            {
                build(a,b);
                build(b,a);
                fuck[a][b] = fuck[b][a] = true;
            }
        }
        ok = false;
        for(i = 1;i <= n && !ok;i ++)
        {
            memset(flag,false,sizeof(flag));
            flag[i] = true;
            dfs(i,1);
        }
    }
    return 0;
}

虽然此题一定有解,但并不能成为不带回溯的理由。

不信可以试试这组数据:

6 10
1 2
2 6
1 6
5 1
1 4
6 3
5 2
2 4
3 5
4 3

这题数据弱了,所以不带回溯枚举起点也是可以AC的,但AC并不代表就是正确的。

友情鸣谢:

感谢@kill_trick 帮忙找到了第一个问题。

感谢u010228612 提供上面一组数据。

还是太年轻了啊啊。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值