122. The book

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

There is a group of N (2<=N<=1000) people which are numbered 1 through N, and everyone of them has not less than [ (N+1) / 2 ] friends. A man with number 1 has the book, which others want to read. Write the program which finds a way of transferring the book so that it will visit every man only once, passing from the friend to the friend, and, at last, has come back to the owner. Note: if A is a friend of B then B is a friend of A.

Input

First line of input contains number N. Next N lines contain information about friendships. (i+1)-th line of input contains a list of friends of i-th man.

Output

If there is no solution then your program must output 'No solution'.   Else your program must output exactly N+1 number: this sequence should begin and should come to end by number 1, any two neighbours in it should be friends, and any two elements in it, except for the first and last, should not repeat.

Sample Input

4
2 3
1 4
1 4
2 3

Sample Output

1 3 4 2 1

1.找到一条不能延伸的路径,此时设左边为st,右边为ed,那么一定满足的性质是,st和ed所能到达的所有点都在这条路上,又由于这个图的性质,两点度数加和一定大于n+1,
根据鸽巢原理,总能找到一个点j,使得j连接ed,nxt[j]连接st,这时候删除j->nxt[j],得到一个环
2.找到一个在环外的点,拆环并且延伸路径
重复这两步直到形成长度为n的环
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1001;
int n;
bool vis[maxn],use[maxn];
int G[maxn][maxn],len[maxn],pre[maxn],nxt[maxn],st,ed;
int predfs(int cnt){
vis[st]=true;
for(int i=0;i<len[st];i++){
int to=G[st][i];
if(!vis[to]){pre[st]=to;nxt[to]=st;st=to;return predfs(cnt+1);}
}
return cnt;
}
int nxtdfs(int cnt){
vis[ed]=true;
for(int i=0;i<len[ed];i++){
int to=G[ed][i];
if(!vis[to]){nxt[ed]=to;pre[to]=ed;ed=to;return nxtdfs(cnt+1);}
}
return cnt;
}
bool getloop(){
if(st==ed)return true;
for(int i=0;i<len[ed];i++){
int to=G[ed][i];
if(to==st){pre[st]=ed;nxt[ed]=st;return true;}
for(int j=0;j<len[nxt[to]];j++){
int jto=G[nxt[to]][j];
int tt=nxt[to];
if(jto==st){
int tmp;
while(st!=ed){
nxt[ed]=to;
tmp=pre[to];
pre[to]=ed;
ed=to;
to=tmp;
}
nxt[st]=tt;
pre[tt]=st;
return true;
}
}
}
return false;
}
bool breakloop(){
for(int i=1;i<=n;i++){
if(!vis[i]){
for(int j=0;j<len[i];j++){
if(vis[G[i][j]]){
st=i;
ed=pre[G[i][j]];
nxt[i]=G[i][j];
pre[G[i][j]]=st;
return true;
}
}
}
}
return false;
}
void printloop(){
int sst=1,tmp=1;
bool fl=false;
while(sst!=tmp||!fl){
printf("%d ",tmp);
if(nxt[tmp]==tmp)break;
tmp=nxt[tmp];
fl=true;
}
puts("1");
}
void addedge(int f,int t){
G[f][len[f]++]=t;
}
int main(){
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++){
int tmpt;
while(scanf("%d",&tmpt)==1){
if(tmpt!=i)addedge(i,tmpt);
if(i==1)use[tmpt]=true;
char ch=getchar();
if(ch=='\n')break;
}
}
int len=1;
st=ed=1;
while(len<n){
len+=predfs(0);
len+=nxtdfs(0);
if(len==n)break;
if(!getloop())break;
if(!breakloop())break;
len++;
}
getloop();
if(len<n)puts("No solution");
printloop();
return 0;
}

  

sgu 122. The book 满足ore性质的汉密尔顿回路 难度:2的更多相关文章

  1. SGU 156 Strange Graph 欧拉回路,思路,汉密尔顿回路 难度:3

    http://acm.sgu.ru/problem.php?contest=0&problem=156 这道题有两种点 1. 度数>2 在团中的点,一定连接一个度数为2的点 2. 度数等 ...

  2. The sum - SGU 122(斐波那契前N项和)

    直接上代码....... ======================================================================================= ...

  3. SGU 122.The book (哈密顿回路)

    题目描述 有一群人从1到N标号,而且这群人中每个人的朋友个数不少于 (N+1)/2 个. 编号为1的人有一本其他人都想阅读的书. 写一个程序,找到一种传阅顺序使得书本只经过每个人手中一次,并且一个人只 ...

  4. 今日SGU 5.27

    SGU 122 题意:给你n个人,每个人有大于 N / 2(向上取整)的朋友,问你1这个人有一个书,每个人都想看,只能从朋友之间传递,然后最后回到了1这个人,问你 是否有解,然后有解输出路径 收获:哈 ...

  5. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  6. P、NP、NPC、NPH问题的区别和联系

    时间复杂度 时间复杂度描述了当输入规模变大时,程序运行时间的变化程度,通常使用\(O\)来表示.比如单层循环的时间复杂度为\(O(n)\),也就是说程序运行的时间随着输入规模的增大线性增长,两层循环的 ...

  7. Pow共识算法

    谈到哈希算法,每个程序员都不陌生,但是谈到比特币共识算法PoW,如果没有接触过的技术人员可能觉得应该会很复杂,毕竟全球的比特币节点数量如此庞大,达成共识的算法应该不会很简单.但其实如果你已掌握哈希算法 ...

  8. 初赛Part2

    初赛 时间复杂度 主定理(必考) \[ T(n) = aT(\frac{n}{b})+f(n) \] 其中,\(n\)为问题的规模,\(a\)为递推下子问题的数量,\(\frac{n}{b}\)为每个 ...

  9. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

随机推荐

  1. Python3基础 str 通过拆分字符串与插入新的内容形成新的字符串

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Python3基础 file open+write 对不存在的txt进行创建与写入

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. Java命令使用 jmap,jps,jstack,jstat,jhat,jinfo

    Jmap:可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等 Jmap是一个可以输出所有内存中 ...

  4. IDEA,RubyMine等JetBrains系列软件通用破解教程

    此教程不光适用于IDEA,还可以在RubyMine等JetBrains系列软件使用,亲测可用. (1)下载安装你需要的JetBrains系列软件,安装完最好不要打开,直接finish,断开网络. (2 ...

  5. 'curl' is not recognized as an internal or external command

    使用everything搜索本地的curl.exe发现如下 官网查看最新版本https://curl.haxx.se/windows/ 2019-03-06 最新版本7.64.0 curl-7.64. ...

  6. [JVM] - 继10进制的java.lang.Object查看之后

    cmd清除命令:cls 之后查阅了其它博客,发现这位大神同样也在做JVM,并且我很希望用它的10进制转16进制类来测试一下该解析的10进制是否对应着Object的16进制呢? 这位大神的10进制转16 ...

  7. The way to Go(7): 变量

    参考: Github: Go Github: The way to Go 变量 一般格式:var identifier type. Go在声明变量时将变量的类型放在变量的名称之后: 避免像 C 语言中 ...

  8. setSupportActionBar()方法报错

    在Android开发中,使用ToolBar控件替代ActionBar控件,需要在java代码中使用setSupportActionBar()方法,如下: Toolbar toolbar = (Tool ...

  9. Jmeter工具做性能测试 常见的错误汇总

    在Win机器上用Jmeter做性能测试,汇总下我自身遇到的错误和解决方案 java.net.BindException: Address already in use: JVM_Bind 原因分析:压 ...

  10. RabbitMQ入门_15_访问控制

    参考资料:https://www.rabbitmq.com/access-control.html A. 核心概念 Virtual Host:虚拟主机为 RabbitMQ 中的资源提供了逻辑分组与隔离 ...