国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

The Necklace UVA - 10054 題解(歐拉回路,路徑輸出)

2019-11-08 02:32:11
字體:
來源:轉載
供稿:網友

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. Input The input contains T test cases. The first line of the input contains the integer T. The first line of each test case contains an integer N (5 ≤ N ≤ 1000) giving the number of beads my sister was able to collect. Each of the next N lines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50. Output 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, print N lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For 1 ≤ i ≤ N1, the second integer on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line N must 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. Sample Input 2 5 1 2 2 3 3 4 4 5 5 6 5 2 1 2 2 3 4 3 1 2 4 Sample Output Case #1 some beads may be lost Case #2 2 1 1 3 3 4 4 2 2 2


一串項鏈每顆珠子兩端有兩種顏色,兩顆珠子相鄰端的顏色一樣,然后項鏈斷了,珠子撒了一地,問你收集起來的部分能不能構成一條項鏈(滿足相鄰端的顏色相同),問題變成了可以有多重邊的圖的歐拉回路了,判斷連通和每個點的度來判斷能不能構造歐拉回路,然后dfs輸出路徑。

#include<iostream>#include<string.h>#include<stdio.h>#include<string>#include<vector>#include<algorithm>using namespace std;int n;int f[55],du[55];bool is[55];int head[55], to[2050], nex[2050];bool used[2050];//int lnum = 0;int en;int F(int x){ if (f[x] == x)return x; return f[x] = F(f[x]);}void add(int x,int y){ to[en] = y; nex[en] = head[x]; head[x] = en++;}void dfs(int cnt){ int c=-1; for (int i = head[cnt]; i != -1; i = nex[i]){ if (!used[i]){ c = to[i]; used[i] = 1; used[(i ^ 1)] = 1; dfs(c); printf("%d %d/n", c, cnt); } }}int main(){ int t; int x, y; scanf("%d", &t); for (int cas = 1; cas <= t; cas++){ en = 0; memset(used, 0, sizeof(used)); memset(is, 0, sizeof(is)); scanf("%d", &n); for (int i = 1; i <= 50; i++){ f[i]=i; head[i] = -1; du[i] = 0; } for (int i = 0; i < n; i++){ scanf("%d%d", &x, &y); f[F(x)] = F(y); du[x]++; du[y]++; is[x] = 1; is[y] = 1; add(x, y); add(y, x); } bool h_ans = 1; int cnthead = -1; for (int i = 1; i <= 50; i++){ if (is[i]){ if (cnthead == -1){ cnthead = F(i); } else{ if (F(i) != cnthead){ h_ans = 0; break; } } if (du[i] % 2 != 0){ h_ans = 0; break; } } } if (cas !=1)printf("/n"); printf("Case #%d/n", cas); if (h_ans == 0){ printf("some beads may be lost/n"); } else{ dfs(cnthead); } } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新沂市| 同德县| 侯马市| 任丘市| 遂平县| 三门县| 无极县| 彰化县| 项城市| 蒲城县| 名山县| 兰州市| 阿克苏市| 洮南市| 喀喇沁旗| 江川县| 西林县| 松潘县| 普兰店市| 浙江省| 山西省| 两当县| 乐至县| 崇礼县| 方山县| 洪雅县| 抚远县| 洪江市| 志丹县| 临夏县| 新津县| 绥德县| 承德县| 北海市| 乐东| 灯塔市| 应城市| 光山县| 扶绥县| 信宜市| 长沙市|