国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乌珠穆沁旗| 东山县| 广宗县| 侯马市| 福州市| 攀枝花市| 仙桃市| 南陵县| 汉寿县| 方正县| 万宁市| 赞皇县| 古交市| 黑龙江省| 云林县| 枣阳市| 通海县| 成安县| 巨野县| 南丹县| 西青区| 齐齐哈尔市| 阿拉善右旗| 徐水县| 威信县| 金溪县| 凉城县| 淄博市| 商洛市| 亳州市| 香河县| 延津县| 鄯善县| 华阴市| 西峡县| 塔河县| 新闻| 十堰市| 庄河市| 清苑县| 德昌县|