Algorithm


C. Useful Decomposition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!

He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!

The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.

Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.

Input

The first line contains a single integer n (2n1052≤�≤105) the number of nodes in the tree.

Each of the next n1� − 1 lines contains two integers ai�� and bi�� (1ai,bin1≤��,��≤�aibi��≠��) — the edges of the tree. It is guaranteed that the given edges form a tree.

Output

If there are no decompositions, print the only line containing "No".

Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition m.

Each of the next m lines should contain two integers ui��vi�� (1ui,vin1≤��,��≤�uivi��≠��) denoting that one of the paths in the decomposition is the simple path between nodes ui�� and vi��.

Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.

If there are multiple decompositions, print any.

Examples
input
Copy
4
1 2
2 3
3 4
output
Copy
Yes
1
1 4
input
Copy
6
1 2
2 3
3 4
2 5
3 6
output
Copy
No
input
Copy
5
1 2
1 3
1 4
1 5
output
Copy
Yes
4
1 2
1 3
1 4
1 5
Note

The tree from the first example is shown on the picture below:The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

The tree from the second example is shown on the picture below:We can show that there are no valid decompositions of this tree.

The tree from the third example is shown on the picture below:The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 1;
int n;
bool vis[N];
vector<vector<int> > g;
vector<int> sol;

void DFS(int u) {
  vis[u] = true;

  bool ok = false;
  for(int i = 0, v; i < g[u].size(); ++i) {
    v = g[u][i];
    if(!vis[v]) {
      ok = true;
      DFS(v);
    }
  }

  if(!ok)
    sol.push_back(u + 1);
}

int main() {
  scanf("%d", &n);
  g.resize(n);
  for(int i = 0, a, b; i < n - 1; ++i) {
    scanf("%d %d", &a, &b);
    --a, --b;
    g[a].push_back(b);
    swap(a, b);
    g[a].push_back(b);
  }

  int gt2 = 0;
  for(int i = 0; i < n; ++i)
    if(g[i].size() > 2)
      ++gt2;

  if(gt2 > 1) {
    puts("No");
    return 0;
  }

  puts("Yes");

  if(gt2 == 0) {
    puts("1");
    for(int i = 0; i < g.size(); ++i)
      if(g[i].size() == 1)
        printf("%d ", i + 1);
    puts("");
    return 0;
  }

  int mx = 0, idx;
  for(int i = 0; i < n; ++i) {
    if(g[i].size() > mx) {
      mx = g[i].size();
      idx = i;
    }
  }

  printf("%d\n", mx);

  DFS(idx);
  ++idx;

  for(int i = 0; i < sol.size(); ++i)
    printf("%d %d\n", idx, sol[i]);

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
1 2
2 3
3 4

Output

x
+
cmd
Yes
1
1 4
Advertisements

Demonstration


Codeforces Solution-C. Useful Decomposition-Solution in C, C++, Java, Python ,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+