ICPC Central Europe Regional Contest 2019 C. Bob in Wonderland

A chain, as everybody knows, is made of connected links. Typically, all links are of the sameshape and size. Bob is a blacksmith apprentice, and he is making his own first iridium chain.He follows the traditional formula of chain-making. It says:

• If there is no chain yet, make a link and it will be a piece of your chain.

• If there is a piece of chain, make another link and connect it to one other link in the pieceof chain you already have.

Bob made the first link. Then, each time he made another link, he connected it to some otherlink in his piece of chain, exactly as the formula told him to do.

When he finished, he realized that the object he created did not resemble a usual chain at all.In an effort to straighten the chain, he repeatedly took two links which seemed to be at the endsof the chain and tried to pull them apart as far as he could. But there were some more piecesof the “chain” dangling down from the straightened piece at various places.

It was obvious to Bob that his work is not finished yet and he decided to call the object heproduced the unfinished chain. After some more pondering, Bob came to a conclusion that hehas to break some links and reconnect them to the rest of the unfinished chain more cautiouslyto obtain a straight chain he aims to produce. In a straight chain, each link is connected to atmost two other links and a straight chain cannot be separated into more pieces without breakinga link.

Being now more careful, Bob is going to progress in simple steps. In one step he will choose alink, say A, connected to another link, say B, in the unfinished chain. He will then break A,disconnect it from B and reconnect A to some other link, say C, in the unfinished chain. Ifthere are more links other than B originally connected to A, Bob will keep them connected toA during the whole step.

What is the minimum number of steps Bob has to perform to get a straight chain?

Input Specification

The first line contains one integer N (1 ≤ N ≤ 3 · 10^{5} )N(1≤N≤3⋅10
5
), the number of links in the unfinishedchain. The links are labeled 1, 2, . . . , N1,2,…,N. Each of the next N − 1N−1 lines has two integers denotingthe labels of two connected links in the unfinished chain. The connections are listed in arbitraryorder. The unfinished chain is guaranteed to form only one piece.

Output Specification

Output the minimum number of steps which will turn Bob’s unfinished chain into a straightchain.

樣例輸入1複製
5
4 3
1 2
4 5
3 2
樣例輸出1複製
0
樣例輸入2複製
6
1 3
3 2
3 4
4 5
4 6
樣例輸出2複製
2
樣例輸入3複製
7
1 2
2 3
3 4
4 5
3 6
6 7
樣例輸出3複製
1

題意;
有一棵樹,問最少調整多少邊使得這棵樹變成鏈

思路:
只需要看哪些點的度數大於2即可,一開始看錯題了,以爲是調整成1 2 3 4 這樣的順序。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 6e5 + 7;

vector<int>G[maxn];

int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i < n;i++) {
        int x,y;scanf("%d%d",&x,&y);
        G[x].push_back(y);G[y].push_back(x);
    }
    int ans = 0;
    for(int i = 1;i <= n;i++) {
        if(G[i].size() > 2) {
            ans += G[i].size() - 2;
        }
    }
    printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章