<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        codeforcesRound#259(div2)E解題報告

        來源:懂視網 責編:小采 時間:2020-11-09 08:02:32
        文檔

        codeforcesRound#259(div2)E解題報告

        codeforcesRound#259(div2)E解題報告:E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Su
        推薦度:
        導讀codeforcesRound#259(div2)E解題報告:E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Su

        解法:

        首先我們可以明確一點,這就是一個圖的遍歷,找一個點,設為起點,建立一個搜索遍歷樹,對于樹每一個點,我們完全可以控制奇偶性,假設:

        目前訪問的點為v,父節點為fa,如若點v不符合當前的奇偶性,則就讓父節點到v繞一次,這樣 odd[v] ^= 1, fa[v] ^= 1,這樣我們可以完全保證完全控制子節點,將不符合要求的奇偶性調整成符合要求的奇偶性。同時父節點的奇偶性也在改變。

        通過上述的操作,我們可以每次保證子節點的奇偶性符合要求,然而父節點的奇偶性如若不符合要求,則可以通過調整父節點 與 父節點的父節點來調整奇偶性,這樣我們就可以奇偶性傳遞,最終傳遞到根節點。根節點如若不符合該如何調整呢?由于我們是走遍歷樹,到達葉節點還要回來的,意味著我們要走至少兩次根節點,一次是出發,一次是最后一次回歸。我們可以將根節點 r1 調整到根節點的其中一個子節點r2,使得奇偶性再次得到調整

        代碼:

        #include 
        #include 
        #define N_max 123456
        
        using namespace std;
        
        int n, m, fa[N_max], want[N_max];
        int Odd_n, Odd_x, haveOdd[N_max];
        vector  G[N_max], ans;
        
        int getf(int x) {
        	return (fa[x] == x) ? x : fa[x] = getf(fa[x]);
        }
        void addedge(int x, int y) {
        	G[x].push_back(y);
        }
        
        void init() {
        	scanf("%d%d", &n, &m);
        
        	for (int i = 1; i <= n; i++) fa[i] = i;
        	for (int i = 1; i <= m; i++) {
        	int x, y;
        
        	scanf("%d%d", &x, &y);
        
        	int tmpx=getf(x);
        	int tmpy=getf(y);
        	if (tmpx != tmpy) {
        	fa[tmpx] = tmpy;
        	addedge(x, y);
        	addedge(y, x);
        	}
        	}
        
        	for (int i = 1; i <= n; i++) {
        	scanf("%d", &want[i]);
        
        	if (want[i]) haveOdd[getf(i)] = 1;
        	}
        
        	for (int i = 1; i <= n; i++)
        	if (haveOdd[i]) {
        	Odd_n++;
        	Odd_x = i;
        	}
        }
        
        void dfs(int now, int pre) {
        	ans.push_back(now);
        	want[now] ^= 1;
        
        	for (int i = 0; i < G[now].size(); i++) {
        	int nex = G[now][i];
        	if (nex == pre) continue;
        
        	dfs(nex, now);
        	ans.push_back(now);
        	want[now] ^= 1;
        
        	if (want[nex]) {
        	ans.push_back(nex);
        	want[nex] ^= 1;
        	ans.push_back(now);
        	want[now] ^= 1;
        	}
        	}
        }
        
        void solve() {
        	if (Odd_n == 0) {
        	printf("0\n");
        	return;
        	}
        
        	if (Odd_n > 1) {
        	printf("-1\n");
        	return;
        	}
        
        	dfs(Odd_x, -1);
        	int x = 0;
        	if (want[Odd_x]) x=1;
        
        	printf("%d\n", ans.size() - x);
        	for (int i = x; i < ans.size(); i++)
        	printf("%d ", ans[i]);
        }
        
        int main() {
        	init();
        	solve();
        }

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        codeforcesRound#259(div2)E解題報告

        codeforcesRound#259(div2)E解題報告:E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Su
        推薦度:
        標簽: 報告 解題 round
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产在线精品免费aaa片| 无码的免费不卡毛片视频| 日韩精品人妻系列无码专区免费| 亚洲精品久久久www| 国产91成人精品亚洲精品| 毛片基地免费观看| 国产成人精品日本亚洲网址| 免费看h片的网站| 亚洲情A成黄在线观看动漫软件| 91黑丝国产线观看免费| 亚洲一区二区三区乱码在线欧洲| h视频在线观看免费完整版| 亚洲毛片免费观看| 无码日韩人妻av一区免费| 亚洲日韩国产一区二区三区在线| 免费观看大片毛片| 美女露100%胸无遮挡免费观看| 亚洲第一区精品观看| aa在线免费观看| 亚洲 欧洲 自拍 另类 校园| 亚洲成在人线aⅴ免费毛片| 亚洲性无码AV中文字幕| 国产免费啪嗒啪嗒视频看看| 九九久久国产精品免费热6| 亚洲精品亚洲人成在线观看| 1000部夫妻午夜免费 | 免费羞羞视频网站| 免费人成又黄又爽的视频在线电影| 亚洲精品天堂成人片?V在线播放| 91免费福利视频| 亚洲国产中文在线视频| 成在人线av无码免费高潮喷水| 色播亚洲视频在线观看| 好吊妞788免费视频播放| 免费精品国自产拍在线播放 | 久青草视频在线观看免费| 亚洲人成在线播放网站岛国| 亚洲AV成人片无码网站| 亚洲色成人网站WWW永久| 亚洲一区二区三区免费观看| 麻豆69堂免费视频|