My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure below shows a segment of the necklace:
But, alas! One day, the necklace was torn and the beads were all scattered over the floor. My sister did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect all
of them. Now, she has come to me for help. She wants to know whether it is possible to make a necklace using all the beads she has in the same way her original necklace was made and if so in which order the bids must be put.
Please help me write a program to solve the problem.
The input containsTtest cases. The first line of the input contains the integerT.
The first line of each test case contains an integerN(
) giving
the number of beads my sister was able to collect. Each of the nextNlines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.
For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence ``some beads may be lost" on a
line by itself. Otherwise, printNlines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For
,
the second integer on lineimust be the same as the first integer on linei+ 1. Additionally, the second integer on lineNmust be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable.
Print a blank line between two successive test cases.
2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4
Case #1
some beads may be lost
Case #2
2 1
1 3
3 4
4 2
2 2
Miguel Revilla
2000-12-28
又是一道无向图的欧拉图问题,首先判定是不是欧拉图,再输出,这里用到了一个算法:
Fleury算法:
(1)任取v0∈V(G),令P0=v0;
(2)设Pi=v0e1v1e2...eivi已经行遍,按下面方法来从E(G)-{e1,e2,...,ei}中选
取ei+1:
(a)ei+1与vi想关联;
(b)除非无别的边可供行遍,否则ei+1不应该为Gi=G-{e1,e2,...,ei}中的桥.
(3)当(2)不能再进行时,算法停止。
例如数据:用电脑把每一步输出,大致理解就是随便找个点然后一它为起点开始遍历,到不能继续时,返回上一层,这是侯输出刚才无法继续遍历的那个店,也就是以他为起点,利用dfs的递归返回时的过程输出,这个算法比回溯快些。
#include<iostream>
#include<cstring>
using namespace std;
int grid[100][100];
int du[100];
int m,tag;
void euler(int pos)
{
for(int i=1;i<=50;i++)
{
if(grid[pos][i]>0)
{
grid[pos][i]--;
grid[i][pos]--;
euler(i);
cout<<i<<" "<<pos<<endl;
}
}
}
int main()
{
int n,t;
cin>>n;
for(t=1;t<=n;t++)
{
memset(grid,0,sizeof(grid));
memset(du,0,sizeof(du));
cin>>m;
int i,j,k;
for(i=0;i<m;i++)
{
cin>>j>>k;
grid[j][k]++;
grid[k][j]++;
du[j]++;
du[k]++;
}
tag=1;
for(i=1;i<=50;i++)
if(du[i]%2!=0)
tag=0;
cout<<"Case #"<<t<<endl;
if(tag==0) cout<<"some beads may be lost"<<endl;
else euler(k);
if(t<n) cout<<endl;
}
return 0;
}
分享到:
相关推荐
Unit 15 The necklace
【优化方案】2014届高考英语一轮复习 Unit15 The necklace随堂检测 新人教版必修1,这个标题表明我们正在讨论一个针对2014年高考英语复习的特定教学资源,主题是“Unit15 The necklace”,属于新人教版高中必修第一...
【优化方案】2014届高考英语一轮复习 Unit15 The necklace课时达标检测 新人教版必修1 本资源为针对2014年高考英语复习的专项训练,重点是Unit15 "The Necklace"的内容。这份资料包含了课时达标检测,包括2013年的...
【知识点详解】 1. 作者介绍:Guy de Maupassant(莫泊桑)是19世纪法国著名的小说家和短篇故事作家,出生于1850年8月5日的诺曼底。他对文学有着浓厚的兴趣,早在学生时代就因诗歌获得奖项。他的写作生涯很早就开始...
综上所述,这段代码实现了一个解决方案来解决“Broken Necklace”问题,即如何从一条断开的项链中选取最长的一段连续的红色或蓝色珠子。通过双向遍历的方式统计不同颜色珠子的数量,并最终确定最长的连续段。
本题是USACO竞赛中的一个经典题目,名为"Broken Necklace (beads)",主要涉及字符串处理、动态规划以及贪心算法。题目要求寻找最优的断点,使得收集到相同颜色珠子的最大数量。以下是相关知识点的详细说明: 1. ...
4. **语态错误** - 句子4 "When asked, he _was_ admitted stealing the necklace." 应去掉 "was",因为 "admit" 通常用主动语态,表示承认某事。 5. **形容词误用** - 句子5 "We know playing computer games for ...
数据集-目标检测系列- 项链 检测数据集 necklace >> DataBall 标注文件格式:xml 项目地址:https://github.com/XIAN-HHappy/ultralytics-yolo-webui 通过webui 方式对ultralytics 的 detect 检测任务 进行: ...
- 例句:The necklace looks beautiful on her long neck. 8. **throat** (n.):咽喉;喉咙 - 例句:She has a sore throat from singing too much. 9. **fever** (n.):发烧 - 例句:If you have a fever, you...
春节主题-1.坏掉的项链 Broken Necklace——这边建议全捡起来.py
高一英语教学参考进度表序号 章节 内容 <BR>1 Unit 13 Healthy eating <BR>2 Unit 14 Festivals <BR>3 Unit 15 The necklace <BR>4 Unit 16 Scientists at work <BR>5 Unit 17 Great women <BR>6 Unit 18 New ...
- **例句:** The necklace is a beautiful accessory to her outfit. #### 32. accident (noun) **释义:**事故,意外事件。 - **例句:** A serious traffic accident occurred on the highway. #### 33. ...
- "as"在某些情况下可替代"which",如"The same necklace as you are wearing now",强调比较的对象。 - "whom"作为关系代词在从句中作宾语,如"My English tutor,Mr. Black,________ I admire"。 - "where...
4. "admit doing"的搭配:"When asked, he was admitted stealing the necklace." 此处表示承认做了某事,应用"admit doing"结构,去掉"was"。 5. 形容词修饰名词:"We know playing computer games for a long ...
There is a beautiful necklace on the table. - 书桌上有很多书。There are many books on the desk. - 篮子里有很多苹果。There are a lot of apples in the basket. - 花园里有一只美丽的蝴蝶。There is a ...
如 “The necklace is (made) of glass.” 项链是由玻璃制成的。 - **be of + 抽象名词**:表示主语具有某种特质或价值,相当于 be + 抽象名词对应的形容词。例如,“Sports and games can be of great value/very ...
另一个例子"How much is the necklace worth?"询问项链的货币价值。 - (2) 描述某物的内在价值或是否值得:例如"That city is worth visiting."表示这座城市有参观的价值,"Is the film worth seeing?"则问电影是否...
"She strung the __________ into a beautiful necklace." 此题涉及饰品词汇,答案是B. beads(珠子),因为珠子常被串成项链。 - ( ) 3. "Mr Tan's __________ is driving him to his office." 这里提到的是职业...
例如:"She accessorized her dress with a stylish necklace."(她用一条时尚的项链装饰她的连衣裙。) 9. **adjacent** - 邻近的,相邻的,用于描述地理位置。例如:"Our house is adjacent to the park."(我们...
- **用法示例**:She was accused of stealing the diamond necklace. #### 16. be accustomed to (= be in the habit of, be used to) 习惯于 - **含义**:指习惯于某种情况或行为。 - **用法示例**:He is ...