Search is not available for this dataset
name stringlengths 2 112 | description stringlengths 29 13k | source int64 1 7 | difficulty int64 0 25 | solution stringlengths 7 983k | language stringclasses 4 values |
|---|---|---|---|---|---|
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<long long> day;
void dfs(long long src, vector<vector<pair<long long, long long>>> &adj,
vector<bool> &vis, long long dont = -1, long long par = -1) {
vis[src] = true;
long long cur = 1;
for (auto &x : adj[src]) {
long long dest = x.first, roadIndex = x.second;
if (dest == par) continue;
if (cur == dont) cur++;
day[roadIndex] = cur;
dfs(dest, adj, vis, cur, src);
cur++;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
vector<vector<pair<long long, long long>>> adj(n);
day.resize(n - 1, -1);
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
vector<bool> vis(n);
dfs(0, adj, vis);
map<long long, vector<long long>> m;
for (long long i = 0; i < day.size(); i++) {
m[day[i]].push_back(i);
}
cout << m.size() << '\n';
for (auto &d : m) {
cout << d.second.size() << ' ';
for (auto &x : d.second) cout << x + 1 << ' ';
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
vector<pair<long long, long long> > g[200005];
vector<long long> ans[200005];
long long res;
void dfs(long long x, long long p, long long t) {
long long cnt = 0;
for (auto i : g[x]) {
if (i.first != p) {
cnt++;
cnt += cnt == t;
ans[cnt].push_back(i.second);
dfs(i.first, x, cnt);
res = max(res, cnt);
}
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 1; i <= n - 1; i++) {
long long x, y;
cin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
res = 0;
dfs(1, -1, 0);
cout << res << "\n";
for (long long i = 1; i <= res; i++) {
cout << ans[i].size() << " ";
for (auto j : ans[i]) cout << j << " ";
cout << "\n";
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200200;
vector<int> d[N];
vector<pair<int, int>> g[N];
int n, k;
void go(int cur, int u = -1, int par = -1) {
int day = 0;
for (auto it : g[cur])
if (it.first != par) {
if (day == u) ++day;
d[day].push_back(it.second);
if (day > k) k = day;
go(it.first, day, cur);
++day;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
scanf("%d%d", &x, &y);
--x;
--y;
g[x].emplace_back(y, i);
g[y].emplace_back(x, i);
}
go(0);
printf("%d\n", k + 1);
for (int i = 0; i <= k; ++i)
if (d[i].size() > 0) {
printf("%d", (int)d[i].size());
for (auto e : d[i]) printf(" %d", e + 1);
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long MODD = 1000000007LL;
long long poww(long long x, long long y, long long MODD) {
if (x == 0LL && y == 0LL) {
return 1LL;
}
long long ret = 1;
while (y) {
if (y & 1LL) {
ret *= x;
ret %= MODD;
}
x *= x;
x %= MODD;
y >>= 1;
}
return ret;
}
vector<long long> gra[200005];
long long n;
long long anse = 0;
long long deg[200005];
map<pair<long long, long long>, long long> M1;
map<pair<long long, long long>, long long> M2;
long long tillnow[200005];
long long par[200005];
long long vis[200005];
void bfs(long long node) {
queue<long long> Q;
vis[node] = 1;
Q.push(node);
while (!Q.empty()) {
long long v = Q.front();
Q.pop();
vis[v] = 1;
for (long long u : gra[v]) {
if (vis[u]) continue;
if (tillnow[v] + 1 == par[v]) tillnow[v]++;
if (tillnow[v] + 1 != par[v]) {
M1[pair<long long, long long>(u, v)] = tillnow[v] + 1;
tillnow[v]++;
par[u] = tillnow[v];
vis[u] = 1;
Q.push(u);
}
}
}
}
vector<pair<long long, long long> > edge[200005];
int main() {
cin >> n;
for (long long i = 1; i <= n - 1; ++i) {
long long a, b;
cin >> a >> b;
M2[pair<long long, long long>(a, b)] = i;
M2[pair<long long, long long>(b, a)] = i;
gra[a].push_back(b);
gra[b].push_back(a);
deg[a]++;
deg[b]++;
anse = max(anse, deg[a]);
anse = max(anse, deg[b]);
}
bfs(1);
auto it = M1.begin();
while (it != M1.end()) {
edge[it->second].push_back(it->first);
it++;
}
cout << anse << "\n";
for (long long i = 1; i <= anse; ++i) {
cout << edge[i].size() << " ";
for (pair<long long, long long> j : edge[i]) {
cout << M2[j] << " ";
}
cout << "\n";
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class CF {
static int[] excludeDay;
static int[] nextFreeDay;
static boolean[] isVisited;
static ArrayList<Integer>[] schedule;
static List<Edge>[] adj;
public static void main(String[] args) {
InputReader inputReader = new InputReader(System.in);
PrintWriter printWriter = new PrintWriter(System.out, true);
int n = inputReader.nextInt();
adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
int u = inputReader.nextInt() - 1;
int v = inputReader.nextInt() - 1;
adj[u].add(new Edge(v, i + 1));
adj[v].add(new Edge(u, i + 1));
}
int maxDegree = -1;
for (int i = 0; i < n; i++) {
int nextSize = adj[i].size();
maxDegree = maxDegree > nextSize ? maxDegree : nextSize;
}
schedule = new ArrayList[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
schedule[i] = new ArrayList<>();
}
excludeDay = new int[n];
nextFreeDay= new int[n];
isVisited = new boolean[n];
DFS(0);
printWriter.println(maxDegree);
for (int i = 1; i <= maxDegree; i++) {
printWriter.print(schedule[i].size() + " ");
for (Integer road : schedule[i]) {
printWriter.print(road + " ");
}
printWriter.println();
}
printWriter.close();
}
private static void DFS(int u) {
isVisited[u] = true;
for (Edge e : adj[u]) {
int v = e.getV();
if (!isVisited[v]) {
if (++nextFreeDay[u] != excludeDay[u]) {
excludeDay[v] = nextFreeDay[u];
} else {
excludeDay[v] = ++nextFreeDay[u];
}
schedule[nextFreeDay[u]].add(e.getId());
DFS(v);
}
}
}
private static class Edge {
private int v;
private int id;
public Edge(int v, int id) {
this.v = v;
this.id = id;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
private static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public int[] nextIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; ++i) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; ++i) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; ++i) {
array[i] = nextDouble();
}
return array;
}
public String[] nextStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; ++i) {
array[i] = next();
}
return array;
}
public boolean[][] nextBooleanTable(int rows, int columns, char trueCharacter) {
boolean[][] table = new boolean[rows][columns];
for (int i = 0; i < rows; ++i) {
String row = next();
assert row.length() == columns;
for (int j = 0; j < columns; ++j) {
table[i][j] = (row.charAt(j) == trueCharacter);
}
}
return table;
}
public char[][] nextCharTable(int rows, int columns) {
char[][] table = new char[rows][];
for (int i = 0; i < rows; ++i) {
table[i] = next().toCharArray();
assert table[i].length == columns;
}
return table;
}
public int[][] nextIntTable(int rows, int columns) {
int[][] table = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextInt();
}
}
return table;
}
public long[][] nextLongTable(int rows, int columns) {
long[][] table = new long[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextLong();
}
}
return table;
}
public double[][] nextDoubleTable(int rows, int columns) {
double[][] table = new double[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextDouble();
}
}
return table;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = readLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(readLine());
}
return tokenizer.nextToken();
}
public String readLine() {
String line;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 23;
const int MOD = 1e9 + 7;
const int SZ = 2e5 + 100;
vector<pair<int, int> > G[SZ];
vector<int> ans[SZ];
int nr[SZ];
void dfs(int v, int par) {
int p = 0;
for (auto& it : G[v]) {
int u = it.first, id = it.second;
if (u == par) continue;
++p;
if (p == nr[v]) ++p;
ans[p].push_back(id);
nr[u] = p;
dfs(u, v);
}
}
int main() {
ios_base::sync_with_stdio(0);
int n, a, b, st = 1;
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> a >> b;
G[a].emplace_back(b, i);
G[b].emplace_back(a, i);
if (G[a].size() > G[st].size()) {
st = a;
}
if (G[b].size() > G[st].size()) {
st = b;
}
}
dfs(1, -1);
stringstream ss;
ss << G[st].size() << "\n";
for (int i = 1; i <= G[st].size(); ++i) {
ss << ans[i].size();
for (auto it : ans[i]) {
ss << " " << it;
}
ss << "\n";
}
cout << ss.str();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 212345;
vector<pair<int, int> > v[N];
bool used[N];
set<int> st[N];
vector<int> ans[N];
int tt = 0;
void dfs(int x) {
used[x] = 1;
int cnt = 1;
for (int i = 0; i < v[x].size(); i++) {
int to = v[x][i].first;
int ti = v[x][i].second;
if (!used[to]) {
if (!st[x].count(cnt)) {
st[to].insert(cnt);
st[x].insert(cnt);
ans[cnt].push_back(ti);
} else {
cnt++;
st[to].insert(cnt);
st[x].insert(cnt);
ans[cnt].push_back(ti);
}
tt = max(tt, cnt);
cnt++;
dfs(to);
}
}
}
int main() {
int n, m, i, j, x, y;
cin >> n;
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(make_pair(y, i));
v[y].push_back(make_pair(x, i));
}
int id = 0, mx = 0;
for (i = 1; i <= n; i++) {
if (mx < v[x].size()) {
mx = v[x].size();
id = i;
}
}
dfs(id);
cout << tt << endl;
for (i = 1; i <= tt; i++) {
cout << ans[i].size() << " ";
for (j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct par {
int ver, id;
par(){};
par(int _ver, int _id) : ver(_ver), id(_id){};
};
int i, j, n, m, maxi, x, y, id, b[300000];
vector<par> a[300000];
vector<int> ans[300000];
void DFS(int x, int k) {
b[x] = 1;
int cnt = 0;
for (int i = 0; i < (int)a[x].size(); i++)
if (b[a[x][i].ver] == 0) {
if (cnt + 1 == k)
cnt += 2;
else
cnt++;
ans[cnt].push_back(a[x][i].id);
DFS(a[x][i].ver, cnt);
}
maxi = max(maxi, cnt);
}
int main() {
cin >> n;
for (i = 1; i <= n - 1; i++) {
cin >> x >> y;
++id;
a[x].push_back(par(y, id));
a[y].push_back(par(x, id));
}
DFS(1, 0);
cout << maxi << endl;
for (i = 1; i <= maxi; i++) {
cout << ans[i].size() << ' ';
for (j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' ';
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
long long n, m, t, l, r, x, y;
vector<long long> a[200005];
bool vis[200005];
bool maked[200005];
vector<long long> ans[200005];
map<pair<long long, long long>, long long> ind;
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
ind[make_pair(x, y)] = i + 1;
ind[make_pair(y, x)] = i + 1;
}
long long cnt = 0;
queue<pair<long long, long long> > q;
q.push(make_pair(1, 0));
while (!q.empty()) {
pair<long long, long long> pp = q.front();
long long v = pp.first;
long long cur = pp.second;
q.pop();
if (vis[v]) continue;
vis[v] = 1;
long long k = 1;
for (int i = 0; i < a[v].size(); i++) {
if (vis[a[v][i]]) continue;
if (cur == k) k++;
cnt = max(cnt, k);
q.push(make_pair(a[v][i], k));
ans[k].push_back(ind[make_pair(v, a[v][i])]);
k++;
}
}
cout << cnt << endl;
for (int k = 1; k <= cnt; k++) {
cout << ans[k].size() << " ";
for (int j = 0; j < ans[k].size(); j++) {
cout << ans[k][j] << " ";
}
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second != b.second) return a.second > b.second;
return a.first < b.first;
};
class graphal {
public:
int n, mx = 0;
vector<pair<int, int> > *ed;
vector<int> *ans;
int *taken;
graphal(int n) {
this->n = n;
ed = new vector<pair<int, int> >[n];
taken = new int[n + 1]();
ans = new vector<int>[n + 1]();
}
~graphal() { delete[] ed; }
void add(int a, int b, int ix) {
ed[a].push_back(pair<int, int>(b, ix));
ed[b].push_back(pair<int, int>(a, ix));
mx = max(mx, (int)max(ed[a].size(), ed[b].size()));
}
void dfs(int ix, int du, int pix) {
int dtu = 0;
for (auto i : ed[ix])
if (i.first != pix) {
if (dtu == du) dtu++;
ans[dtu].push_back(i.second);
dfs(i.first, dtu, ix);
dtu++;
}
}
void doit() {
dfs(0, -1, -1);
printf("%d\n", mx);
for (int i = 0; i < mx; i++) {
printf("%lu ", ans[i].size());
for (auto j : ans[i]) printf("%d ", j);
printf("\n");
}
}
};
int main() {
int n, a, b;
scanf("%d", &n);
graphal g(n);
for (auto i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
a--, b--;
g.add(a, b, i + 1);
}
g.doit();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
struct node {
vector<int> children;
vector<int> road;
int color;
int parentDay;
};
node lst[N];
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
lst[i].color = lst[i].parentDay = 0;
}
int ans = 0;
for (i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
lst[u].children.push_back(v);
lst[v].children.push_back(u);
lst[u].road.push_back(i);
lst[v].road.push_back(i);
}
for (i = 1; i <= n; i++) ans = max(ans, (int)lst[i].children.size());
vector<vector<int> > v(ans + 1);
queue<int> q;
q.push(1);
lst[1].color = 1;
while (!q.empty()) {
int t = q.front();
q.pop();
j = 1;
for (i = 0; i < lst[t].children.size(); i++) {
int idx = lst[t].children[i];
if (lst[idx].color == 0) {
if (j == lst[t].parentDay) j++;
q.push(idx);
lst[idx].color = 1;
v[j].push_back(lst[t].road[i]);
lst[idx].parentDay = j++;
}
}
}
printf("%d\n", ans);
for (i = 1; i <= ans; i++) {
printf("%d", v[i].size());
for (j = 0; j < v[i].size(); j++) printf(" %d", v[i][j]);
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct Node {
vector<int> neib;
vector<int> ednum;
bool done;
};
Node* nodes;
vector<int>* days;
void dfs(int nodeNum, int parentDay) {
int day = 0;
nodes[nodeNum].done = true;
for (int i = 0; i < (int)nodes[nodeNum].neib.size(); i++) {
if (day == parentDay) day++;
if (nodes[nodes[nodeNum].neib[i]].done == true) {
continue;
}
days[day].push_back(nodes[nodeNum].ednum[i]);
dfs(nodes[nodeNum].neib[i], day);
day++;
}
}
int main(void) {
int nodesCount;
int edgesCount;
cin >> nodesCount;
if (nodesCount == 0) {
return 0;
}
edgesCount = nodesCount - 1;
nodes = new Node[nodesCount];
for (int i = 0; i < edgesCount; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
nodes[a].neib.push_back(b);
nodes[b].neib.push_back(a);
nodes[a].ednum.push_back(i + 1);
nodes[b].ednum.push_back(i + 1);
}
int maxi = 0;
for (int i = 0; i < nodesCount; i++) {
nodes[i].done = false;
maxi = max((int)nodes[i].neib.size(), maxi);
}
days = new vector<int>[maxi];
dfs(0, maxi + 5);
cout << maxi << endl;
for (int i = 0; i < maxi; i++) {
cout << days[i].size() << " ";
for (int j = 0; j < days[i].size(); j++) {
cout << days[i][j] << " ";
}
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.util.*;
public class C {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
static class Edge {
int to, id;
public Edge(int to, int id) {
this.to = to;
this.id = id;
}
}
List<Edge>[] g;
int[] col;
void dfs(int v, int p, int forbColor) {
int ptr = 0;
for (Edge e : g[v]) {
if (e.to == p) {
continue;
}
while (ptr == forbColor) {
ptr++;
}
col[e.id] = ptr;
dfs(e.to, v, ptr);
ptr++;
}
}
void solve() throws IOException {
int n = nextInt();
g = new List[n];
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
int v1 = nextInt() - 1;
int v2 = nextInt() - 1;
g[v1].add(new Edge(v2, i));
g[v2].add(new Edge(v1, i));
}
col = new int[n - 1];
dfs(0, -1, -1);
int size = 0;
for (int i = 0; i < n - 1; i++) {
size = Math.max(size, col[i] + 1);
}
List<Integer>[] outp = new List[size];
for (int i = 0; i < size; i++) {
outp[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
outp[col[i]].add(i);
}
out.println(size);
for (List<Integer> lst : outp) {
out.print(lst.size() + " ");
for (int x : lst) {
out.print((x + 1) + " ");
}
out.println();
}
}
C() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new C();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int INF = 1e9 + 7;
bool used[MAXN];
vector<pair<int, int>> G[MAXN];
vector<int> roads[MAXN];
int dfs(int v, int day) {
used[v] = 1;
int ans = G[v].size();
int firstday = day;
int toadd = -1;
for (int i = 0; i < G[v].size(); ++i)
if (!used[G[v][i].first]) {
day += toadd;
if (day < 0) {
day = firstday + 1;
toadd = 1;
}
roads[day].push_back(G[v][i].second);
ans = max(ans, dfs(G[v][i].first, day));
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(make_pair(v, i));
G[v].push_back(make_pair(u, i));
}
int t = dfs(1, -1);
printf("%d", t);
for (int i = 0; i < t; ++i) {
printf("\n%d ", roads[i].size());
for (int j = 0; j < roads[i].size(); ++j) printf("%d ", roads[i][j]);
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:128777216")
using namespace std;
const long long LINF = 1000000000000000000LL;
const int INF = 1000000000;
const long double eps = 1e-9;
const long double PI = 3.1415926535897932384626433832795l;
void prepare(string s) {
if (s.length() != 0) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
}
const int NMAX = 200005;
struct Edge {
int id, v;
Edge(int _id, int _v) {
id = _id;
v = _v;
}
};
vector<Edge> g[NMAX];
int n;
vector<vector<int>> ans;
void read() {
scanf("%d", &n);
for (int i = 0; i < (int)(n - 1); i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
g[a].push_back(Edge(i + 1, b));
g[b].push_back(Edge(i + 1, a));
}
}
void dfs(int v, int p, int day) {
int cur_day = 0;
for (Edge e : g[v]) {
int u = e.v;
int id = e.id;
if (u == p) continue;
if (cur_day == day) cur_day++;
ans[cur_day].push_back(id);
dfs(u, v, cur_day);
cur_day++;
}
}
void solve() {
int ans_k = 0;
for (int i = 0; i < (int)(n); i++) ans_k = max(ans_k, (int)((g[i]).size()));
printf("%d\n", ans_k);
ans.resize(ans_k, vector<int>());
dfs(0, -1, -1);
for (int i = 0; i < (int)((int)((ans).size())); i++) {
printf("%d", (int)((ans[i]).size()));
for (int j = 0; j < (int)((int)((ans[i]).size())); j++) {
printf(" %d", ans[i][j]);
}
printf("\n");
}
}
int main() {
prepare("");
read();
solve();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const string nameFiles = "";
void error(vector<string>::iterator) {}
template <typename T, typename... Args>
void error(vector<string>::iterator cur_var, T a, Args... args) {
cerr << cur_var->substr((*cur_var)[0] == ' ') << " = " << a << endl;
error(++cur_var, args...);
}
vector<string> split(string const& str, char c) {
vector<string> res;
stringstream ss(str);
string x;
while (getline(ss, x, c)) res.emplace_back(x);
return move(res);
}
string to_str(int x) {
static char buff[64];
sprintf(buff, "%d", x);
return std::string(buff);
}
struct Ver {
int to, id;
};
int n;
vector<vector<Ver>> g;
vector<vector<int>> res;
vector<bool> used;
void dfs(int x, int d = -1) {
used[x] = true;
int cur_d = -1;
for (Ver& y : g[x]) {
if (!used[y.to]) {
++cur_d;
cur_d += cur_d == d;
res[cur_d].push_back(y.id);
dfs(y.to, cur_d);
}
}
}
int main() {
if (!nameFiles.empty()) {
freopen((nameFiles + ".in").c_str(), "r", stdin);
freopen((nameFiles + ".out").c_str(), "w", stdout);
}
scanf("%d", &n);
g.resize(n);
used.resize(n, false);
int max_d = 0;
for (int i = 0, f, t; i < n - 1; ++i) {
scanf("%d %d", &f, &t);
f--, t--;
g[f].push_back({t, i + 1});
g[t].push_back({f, i + 1});
max_d = max(max_d, (int)g[f].size());
max_d = max(max_d, (int)g[t].size());
}
res.resize(max_d);
dfs(0);
printf("%d\n", max_d);
for (int i = 0; i < max_d; ++i) {
printf("%d ", (int)res[i].size());
for (int n : res[i]) printf("%d ", n);
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> adj[200009], num[200009], solution[200009];
int degree[200009];
void dfs(int u, int p, int upcolor) {
vector<int> v;
int timer = 0;
for (int i = 0; i < adj[u].size(); i++)
if (adj[u][i] != p) {
timer++;
if (timer == upcolor) {
timer++;
}
v.push_back(timer);
} else {
v.push_back(0);
}
for (int i = 0; i < adj[u].size(); i++)
if (adj[u][i] != p) {
solution[v[i]].push_back(num[u][i]);
dfs(adj[u][i], u, v[i]);
}
}
int main() {
int n, x, y, madeg = 0;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
num[x].push_back(i + 1);
num[y].push_back(i + 1);
degree[x]++;
degree[y]++;
madeg = max(max(degree[x], degree[y]), madeg);
}
cout << madeg << endl;
dfs(1, 1, 0);
for (int i = 1; i <= madeg; i++) {
cout << solution[i].size() << " ";
for (int j = 0; j < solution[i].size(); j++) {
cout << solution[i][j] << " ";
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> day;
void dfs(int src, vector<vector<pair<int, int>>> &adj, vector<bool> &vis,
int dont = -1, int par = -1) {
vis[src] = true;
int cur = 1;
for (auto &x : adj[src]) {
int dest = x.first, roadIndex = x.second;
if (dest == par) continue;
if (cur == dont) cur++;
day[roadIndex] = cur;
dfs(dest, adj, vis, cur, src);
cur++;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<pair<int, int>>> adj(n);
day.resize(n - 1, -1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
vector<bool> vis(n);
dfs(0, adj, vis);
map<int, vector<int>> m;
for (int i = 0; i < day.size(); i++) {
m[day[i]].push_back(i);
}
cout << m.size() << '\n';
for (auto &d : m) {
cout << d.second.size() << ' ';
for (auto &x : d.second) cout << x + 1 << ' ';
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int x;
vector<int> g[200010];
map<pair<int, int>, int> ans;
map<pair<int, int>, int> pos;
bool cmp(int x, int y) {
if (g[x] >= g[y]) return false;
return true;
}
vector<vector<int> > res;
bool u[200010];
int main() {
cin >> x;
for (int i = 0; i < x - 1; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
g[l].push_back(r);
g[r].push_back(l);
pos[{l, r}] = i + 1;
pos[{r, l}] = i + 1;
}
int ind = 0;
for (int i = 0; i < x; i++) {
sort(g[i].begin(), g[i].end(), cmp);
if (g[ind].size() < g[i].size()) ind = i;
}
queue<pair<int, int> > q;
q.push({0, ind});
int kek = 0;
while (!q.empty()) {
int now;
int p;
auto m = q.front();
q.pop();
now = m.second;
p = m.first;
if (!u[now]) {
u[now] = true;
int f = 1;
for (int i = 0; i < g[now].size(); i++) {
if (p == f) f++;
if (!u[g[now][i]]) {
ans[{now, g[now][i]}] = f;
q.push({f, g[now][i]});
f++;
}
}
kek = max(kek, f);
}
}
for (int i = 0; i < kek - 1; i++) res.push_back(vector<int>());
for (auto i = ans.begin(); i != ans.end(); i++) {
res[i->second - 1].push_back(pos[i->first]);
}
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i].size() << ' ';
for (int j = 0; j < res[i].size(); j++) {
cout << res[i][j] << ' ';
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> adj[300001];
vector<int> parent(200001);
int cma = 0;
vector<int> make[200001];
vector<bool> visited(200001);
void dfs(int v, int p = -1) {
visited[v] = 1;
int cid = 0;
for (auto it : adj[v]) {
if (!visited[it.first]) {
if (cid == p) cid++;
make[cid].push_back(it.second);
dfs(it.first, cid);
cid++;
}
}
}
void solve() {
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
ans = max(ans, (int)adj[a].size());
ans = max(ans, (int)adj[b].size());
}
dfs(1);
cout << ans << endl;
for (int i = 0; i < ans; i++) {
cout << make[i].size() << " ";
for (auto it : make[i]) cout << it + 1 << " ";
cout << endl;
}
cout << endl;
}
int main() {
int t = 1;
while (t--) solve();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = (int)3e5;
int n;
vector<int> g[N];
vector<int> day[N];
struct edge {
int a, b;
} E[N];
void dfs(int v, int e, int bad) {
int curDay = 0;
for (int id : g[v]) {
if (id == e) continue;
if (++curDay == bad) curDay++;
day[curDay].push_back(id);
dfs((E[id].a ^ E[id].b ^ v), id, curDay);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
g[a].push_back(i);
g[b].push_back(i);
E[i].a = a;
E[i].b = b;
}
int mx = 1;
for (int i = 1; i <= n; i++)
if (((int)g[mx].size()) < ((int)g[i].size())) mx = i;
dfs(mx, 0, 0);
printf("%d\n", ((int)g[mx].size()));
for (int i = 1; i <= ((int)g[mx].size()); i++) {
printf("%d", ((int)day[i].size()));
for (int j : day[i]) printf(" %d", j);
puts("");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
int N, head[200001], next[399999], to[399999], id[399999], E, O, q[200001],
fa[200001], d[200001];
std::vector<int> V[200001];
int main() {
scanf("%d", &N);
for (int i = 1, u, v; i < N; i++) {
scanf("%d%d", &u, &v);
d[u]++;
d[v]++;
next[++E] = head[u], to[E] = v, id[E] = i, head[u] = E;
next[++E] = head[v], to[E] = u, id[E] = i, head[v] = E;
}
O = *std::max_element(d + 1, d + N + 1);
q[1] = 1;
d[1] = O;
int H = 0, T = 1, u;
while (H < T)
for (int e = head[u = q[++H]], z = d[u]; e; e = next[e])
if (to[e] != fa[u]) {
fa[q[++T] = to[e]] = u;
z = z == O ? 1 : z + 1;
d[to[e]] = z;
V[d[to[e]]].push_back(id[e]);
}
printf("%d\n", O);
for (int i = 1; i <= O; i++) {
printf("%d", int(V[i].size()));
for (int j : V[i]) printf(" %d", j);
putchar(10);
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
typedef struct lnod {
int nod, nr;
lnod *next;
} * nod;
int i, n, x, y, timer[200005], nr[200005], ans;
nod lda[200005], rs[200005];
void add(int x, int z, nod &y) {
nod p = new lnod;
p->nod = x;
p->nr = z;
p->next = y;
y = p;
}
void dfs(int x, int tata) {
int time = 1;
for (nod p = lda[x]; p; p = p->next)
if (p->nod != tata) {
if (timer[x] == time) ++time;
timer[p->nod] = time;
++nr[time];
add(p->nr, 0, rs[time]);
++time;
}
ans = max(ans, time - 1);
for (nod p = lda[x]; p; p = p->next)
if (p->nod != tata) dfs(p->nod, x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (i = 2; i <= n; ++i) {
cin >> x >> y;
add(x, i - 1, lda[y]);
add(y, i - 1, lda[x]);
}
dfs(1, 1);
cout << ans << '\n';
for (i = 1; i <= ans; ++i, cout << '\n') {
cout << nr[i] << ' ';
for (nod p = rs[i]; p; p = p->next) cout << p->nod << ' ';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int du[N];
vector<int> g[N];
struct Edge {
int from, to, nex;
} edge[N];
int h[N], idx, ans;
void add(int u, int v) {
Edge E = {u, v, h[u]};
edge[idx] = E;
h[u] = idx++;
}
int n;
void dfs(int u, int fa, int last) {
int j = 1;
for (int i = h[u]; ~i; i = edge[i].nex) {
int v = edge[i].to;
if (v == fa) continue;
if (j == last) ++j;
g[j].push_back(i / 2 + 1);
dfs(v, u, j);
++j;
}
return;
}
int main() {
memset(h, -1, sizeof(h));
idx = 0;
ans = 0;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
du[u]++, du[v]++;
ans = max(ans, max(du[u], du[v]));
add(u, v);
add(v, u);
}
cout << ans << endl;
dfs(1, -1, 0);
for (int i = 1; i <= ans; ++i) {
printf("%d ", g[i].size());
for (int j = 0; j < g[i].size(); ++j) printf("%d ", g[i][j]);
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void traverse(int index, int parent_index, int parent_color,
vector<vector<int> > &G_to, vector<vector<int> > &G_idx,
vector<vector<int> > &ans) {
int c = 0;
for (int j = 0; j < G_to[index].size(); j++) {
if (parent_index == G_to[index][j]) {
continue;
}
if (parent_color == c) {
c++;
}
ans[c].push_back(G_idx[index][j] + 1);
traverse(G_to[index][j], index, c, G_to, G_idx, ans);
c++;
}
}
int main() {
int n;
cin >> n;
vector<vector<int> > G_to(n);
vector<vector<int> > G_idx(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
G_to[u].push_back(v);
G_idx[u].push_back(i);
G_to[v].push_back(u);
G_idx[v].push_back(i);
}
int fattest = 0;
for (int i = 1; i < n; i++) {
if (G_to[i].size() > G_to[fattest].size()) {
fattest = i;
}
}
vector<vector<int> > ans(G_to[fattest].size());
traverse(fattest, -1, -1, G_to, G_idx, ans);
cout << ans.size() << std::endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].size() << " ";
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << " ";
}
cout << std::endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 7;
long long int ord[3 * N] = {0}, visit[3 * N] = {0}, indeg[3 * N] = {0}, f = 0;
vector<long long int> ans[2 * N];
map<pair<long long int, long long int>, long long int> mp;
void dfs(vector<long long int> vec[], long long int a) {
long long int cnt = 1;
for (long long int i = 0; i < vec[a].size(); i++) {
if (visit[vec[a][i]]) continue;
if (cnt == ord[a] && f) cnt++;
if (!f) f++;
visit[vec[a][i]]++;
ord[vec[a][i]] = cnt;
ans[cnt].push_back(mp[make_pair(a, vec[a][i])]);
cnt++;
dfs(vec, vec[a][i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
long long int n;
cin >> n;
long long int m = n - 1;
vector<long long int> vec[n];
long long int c = 1;
for (long long int i = 0; i < m; i++) {
long long int a, b;
cin >> a >> b;
a--;
b--;
vec[a].push_back(b);
vec[b].push_back(a);
mp[make_pair(a, b)] = c;
mp[make_pair(b, a)] = c;
c++;
indeg[a]++;
indeg[b]++;
}
long long int st = -1, deg = 0;
for (long long int i = 0; i < n; i++) {
if (indeg[i] > deg) {
deg = indeg[i];
st = i;
}
}
cout << indeg[st] << endl;
visit[st]++;
ord[st] = 1;
dfs(vec, st);
for (long long int i = 1; i <= deg; i++) {
cout << ans[i].size() << " ";
for (long long int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " ";
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> day;
void dfs(int src, vector<vector<pair<int, int>>> &adj, int dont = 0,
int par = -1) {
int cur = 1;
for (auto &x : adj[src]) {
if (x.first == par) continue;
if (cur == dont) cur++;
day[x.second] = cur;
dfs(x.first, adj, cur, src);
cur++;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<pair<int, int>>> adj(n);
day.resize(n - 1, -1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
vector<bool> vis(n);
dfs(0, adj);
map<int, vector<int>> m;
for (int i = 0; i < day.size(); i++) {
m[day[i]].push_back(i);
}
cout << m.size() << '\n';
for (auto &d : m) {
cout << d.second.size() << ' ';
for (auto &x : d.second) cout << x + 1 << ' ';
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct node {
int v, next, num;
} e[maxn << 1];
int n;
int cnt, head[maxn];
inline void add(int x, int y, int num) {
e[++cnt].v = y;
e[cnt].next = head[x];
head[x] = cnt;
e[cnt].num = num;
}
int du[maxn], ans;
vector<int> pth[maxn];
void dfs(int x, int fa, int last) {
int tot = 1;
for (int i = head[x]; i; i = e[i].next) {
int to = e[i].v;
if (to == fa) continue;
if (tot == last) tot++;
pth[tot].push_back(e[i].num);
dfs(to, x, tot++);
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y, i), add(y, x, i);
du[x]++, du[y]++;
}
for (int i = 1; i <= n; i++) {
if (du[i] > ans) ans = du[i];
}
dfs(1, 0, 0);
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
int sz = pth[i].size();
cout << sz << " ";
for (int j = 0; j < sz; j++) cout << pth[i][j] << " ";
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> edge_no;
int max_day = 0;
vector<int> build_schedule[200001];
void addEdge(vector<int> adj[], int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void DFSUtil(int u, vector<int> adj[], vector<bool> &visited, int day) {
visited[u] = true;
int build_day = 1;
for (int i = 0; i < adj[u].size(); i++)
if (visited[adj[u][i]] == false) {
if (build_day != day) {
max_day = max(max_day, build_day);
build_schedule[build_day].push_back(edge_no[make_pair(u, adj[u][i])]);
DFSUtil(adj[u][i], adj, visited, build_day++);
} else {
max_day = max(max_day, build_day + 1);
build_schedule[build_day + 1].push_back(
edge_no[make_pair(u, adj[u][i])]);
DFSUtil(adj[u][i], adj, visited, ++build_day);
build_day++;
}
}
}
void DFS(vector<int> adj[], int V) {
vector<bool> visited(V, false);
for (int u = 1; u < V; u++)
if (visited[u] == false) DFSUtil(u, adj, visited, 0);
}
int main() {
int n, a, b;
scanf("%d", &n);
vector<int> adj[n + 1];
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
addEdge(adj, a, b);
edge_no.insert(pair<pair<int, int>, int>(make_pair(a, b), i));
edge_no.insert(pair<pair<int, int>, int>(make_pair(b, a), i));
}
DFS(adj, n + 1);
printf("%d\n", max_day);
for (int i = 1; i <= max_day; i++) {
printf("%d ", build_schedule[i].size());
for (auto it = build_schedule[i].begin(); it != build_schedule[i].end();
it++) {
printf("%d ", *it);
}
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
std::vector<pair<int, int>> adj[200005];
std::vector<int> ans[200005];
void dfs(int n, int u, int v) {
int j = 1;
for (auto i : adj[n]) {
if (j == v) j++;
if (i.first != u) {
ans[j].push_back(i.second);
dfs(i.first, n, j);
j++;
}
}
return;
}
int main() {
int i, j, k, l, m, n, u, v, a, b, c, d, maxi = INT_MIN;
cin >> n;
for (i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
maxi = max(maxi, (int)max(adj[u].size(), adj[v].size()));
}
dfs(1, -1, 0);
cout << maxi << endl;
for (i = 1; i <= maxi; i++) {
cout << ans[i].size() << " ";
for (j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " ";
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > g;
vector<int> day;
void dfs(int v, int edge) {
for (int i = 0, d = 0; i < g[v].size(); ++i, ++d) {
if (g[v][i].second == edge) {
--d;
continue;
}
if (edge >= 0 && d == day[edge]) ++d;
day[g[v][i].second] = d;
dfs(g[v][i].first, g[v][i].second);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(make_pair(v, i));
g[v].push_back(make_pair(u, i));
}
day.assign(n - 1, -1);
dfs(0, -1);
int num_of_days = 0;
for (int i = 0; i < n - 1; ++i) num_of_days = max(num_of_days, day[i] + 1);
vector<vector<int> > ans(num_of_days);
for (int i = 0; i < n - 1; ++i) ans[day[i]].push_back(i + 1);
cout << num_of_days << "\n";
for (int i = 0; i < num_of_days; ++i) {
cout << ans[i].size() << " ";
for (int j = 0; j < ans[i].size(); ++j) cout << ans[i][j] << " ";
cout << "\n";
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
int n;
vector<pair<int, int> > edge[MAXN + 5];
int ind[MAXN + 5];
vector<int> ans[MAXN + 5];
inline void OPEN(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
void dfs(int u, int fa, int No) {
int now = 0;
for (int i = 0; i < edge[u].size(); i++) {
pair<int, int> v = edge[u][i];
if (v.first == fa) continue;
now + 1 == No ? now += 2 : now += 1;
ans[now].push_back(v.second);
dfs(v.first, u, now);
}
}
void Solve(int start) {
int now = 0;
for (int i = 0; i < edge[start].size(); i++) {
pair<int, int> v = edge[start][i];
now++;
ans[now].push_back(v.second);
dfs(v.first, start, now);
}
}
void Print(int start) {
printf("%d\n", ind[start]);
for (int i = 1; i <= ind[start]; i++) {
printf("%d ", ans[i].size());
for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]);
printf("\n");
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(make_pair(v, i));
edge[v].push_back(make_pair(u, i));
ind[u]++, ind[v]++;
}
int start = 1;
for (int i = 1; i <= n; i++)
if (ind[i] > ind[start]) start = i;
Solve(start);
Print(start);
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int n, degree[MAXN];
vector<pair<int, int> > edges[MAXN];
vector<vector<int> > answers;
void dfs(int u, int parent, int day) {
int dayIdx = 0;
for (auto &edge : edges[u])
if (edge.first != parent) {
dayIdx += dayIdx == day;
if (dayIdx >= ((int)(answers).size())) answers.push_back(vector<int>());
answers[dayIdx].push_back(edge.second);
dfs(edge.first, u, dayIdx++);
}
}
int main() {
scanf("%d", &n);
for (int i = (1); i < (n); ++i) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
edges[x].push_back(make_pair(y, i));
edges[y].push_back(make_pair(x, i));
degree[x]++;
degree[y]++;
}
dfs(0, -1, -1);
printf("%d\n", ((int)(answers).size()));
for (auto &answer : answers) {
printf("%d", ((int)(answer).size()));
for (auto &x : answer) printf(" %d", x);
putchar('\n');
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
public class RoadImp{
private static void dfs(Node[] nodes, int n, HashMap<Integer, List<Integer>> res, int cur, int parentLabel, HashSet<Integer> visited){
//System.out.println("cur:"+cur+",parentLabel:"+parentLabel);
int curLabel=1;
visited.add(cur);
for(int i=0; i<nodes[cur].neighbors.size(); i++){
int neighb=nodes[cur].neighbors.get(i);
if(visited.contains(neighb)) continue;
//System.out.println("cur:"+cur+",neighb:"+neighb);
if(curLabel==parentLabel) curLabel++;
if(!res.containsKey(curLabel)){
res.put(curLabel, new ArrayList<Integer>());
}
res.get(curLabel).add(nodes[cur].edgeNumbers.get(i));
dfs(nodes, n, res, neighb, curLabel, visited);
curLabel++;
}
}
public static void main(String[] argv) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine().trim());
Node[] nodes=new Node[n+1];
for(int i=1; i<n; i++){
String[] words=br.readLine().trim().split(" ");
int u=Integer.parseInt(words[0]), v=Integer.parseInt(words[1]);
if(nodes[u]==null){
nodes[u]=new Node(u);
}
if(nodes[v]==null){
nodes[v]=new Node(v);
}
nodes[u].add(v, i);
nodes[v].add(u, i);
}
HashMap<Integer, List<Integer>> res=new HashMap<Integer, List<Integer>>();
dfs(nodes, n, res, 1, -1, new HashSet<Integer>());
System.out.println(res.size());
StringBuilder sb=new StringBuilder();
for(Integer k: res.keySet()){
sb.append(res.get(k).size()+" ");
for(int j=0; j<res.get(k).size(); j++){
sb.append(res.get(k).get(j)+" ");
}
sb.append("\n");
}
System.out.println(sb);
br.close();
}
}
class Node{
int val;
List<Integer> neighbors, edgeNumbers;
public Node(int val){
this.val=val;
this.neighbors=new ArrayList<Integer>();
this.edgeNumbers=new ArrayList<Integer>();
}
public void add(int u, int num){
this.neighbors.add(u);
this.edgeNumbers.add(num);
}
}
| JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
long long f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
read(first);
read(args...);
}
template <typename T>
void write(T arg) {
T x = arg;
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template <typename T, typename... Ts>
void write(T arg, Ts... args) {
write(arg);
if (sizeof...(args) != 0) {
putchar(' ');
write(args...);
}
}
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int N = 200005;
int n;
struct node {
int t, id, col;
};
vector<node> edge[N];
vector<int> ans[N];
void dfs(int u, int fa, int col) {
int x = 1;
for (auto it : edge[u]) {
int v = it.t, id = it.id;
if (v == fa) continue;
if (x == col) x++;
it.col = x;
ans[x].push_back(id);
dfs(v, u, x);
x++;
}
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back({v, i, 0});
edge[v].push_back({u, i, 0});
}
int tot = -1;
for (int i = 1; i <= n; i++) tot = max(tot, (int)edge[i].size());
dfs(1, 0, 0);
cout << tot << endl;
for (int i = 1; i <= tot; i++) {
cout << ans[i].size() << " ";
for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " ";
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void boost() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 2e5 + 5;
vector<int> day[N];
int l[N], r[N];
vector<pair<int, int> > adj[N];
void dfs(int u, int p) {
for (auto v : adj[u]) {
if (v.first != p) {
if (l[u] - 1 > 0) {
l[u]--;
day[l[u]].push_back(v.second);
l[v.first] = r[v.first] = l[u];
dfs(v.first, u);
} else {
r[u]++;
day[r[u]].push_back(v.second);
l[v.first] = r[v.first] = r[u];
dfs(v.first, u);
}
}
}
}
void solve() {
int n;
cin >> n;
int u, v;
for (int i = 1; i < n; ++i) {
cin >> u >> v;
adj[u].push_back(make_pair(v, i));
adj[v].push_back(make_pair(u, i));
}
dfs(1, 0);
int cnt = 0;
for (int i = 1; i < N; ++i) {
if (day[i].size() != 0) cnt++;
}
cout << cnt << endl;
for (int i = 1; i < cnt + 1; ++i) {
cout << day[i].size() << " ";
for (auto x : day[i]) {
cout << x << " ";
}
cout << endl;
}
return;
}
int main() {
boost();
int tc = 1;
while (tc--) solve();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int lim = (int)(2e5 + 5);
int n, ans;
vector<pair<int, int> > v[lim];
vector<int> vec[lim];
void dfs(int x, int back, int y) {
int cn = 0;
for (int i = 0; i < (int)v[x].size(); i++) {
if (v[x][i].first == back) continue;
cn++;
if (cn == y) cn++;
ans = max(ans, cn);
vec[cn].push_back(v[x][i].second);
dfs(v[x][i].first, x, cn);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(make_pair(b, i));
v[b].push_back(make_pair(a, i));
}
dfs(1, -1, -1);
printf("%d\n", ans);
for (int i = 0; i <= n - 1; i++) {
if (vec[i].size()) {
printf("%d ", (int)vec[i].size());
for (int j = 0; j < (int)vec[i].size(); j++) printf("%d ", vec[i][j]);
printf("\n");
}
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
class graphal {
public:
int n, mx = 0;
vector<pair<int, int> > *ed;
vector<int> *ans;
graphal(int n) {
this->n = n;
ed = new vector<pair<int, int> >[n];
ans = new vector<int>[n + 1]();
}
~graphal() {
delete[] ed;
delete[] ans;
}
void add(int a, int b, int ix) {
ed[a].push_back(pair<int, int>(b, ix));
ed[b].push_back(pair<int, int>(a, ix));
mx = max(mx, (int)max(ed[a].size(), ed[b].size()));
}
void dfs(int ix, int du, int pix) {
int dtu = 0;
for (auto i : ed[ix])
if (i.first != pix) {
if (dtu == du) dtu++;
ans[dtu].push_back(i.second);
dfs(i.first, dtu, ix);
dtu++;
}
}
void doit() {
dfs(0, -1, -1);
printf("%d\n", mx);
for (int i = 0; i < mx; i++) {
printf("%lu ", ans[i].size());
for (auto j : ans[i]) printf("%d ", j);
printf("\n");
}
}
};
int main() {
int n, a, b;
scanf("%d", &n);
graphal g(n);
for (auto i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
a--, b--;
g.add(a, b, i + 1);
}
g.doit();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class C extends PrintWriter {
class Road {
final int id, dst;
Road ret;
public Road(int id, int dst) {
this.id = id;
this.dst = dst;
}
}
void run() {
int n = nextInt(), m = 0, s = 0;
List<Road>[] g = new List[n];
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<Road>();
}
for (int id = 1; id < n; id++) {
int u = nextInt() - 1, v = nextInt() - 1;
Road uv = new Road(id, v);
Road vu = new Road(id, u);
uv.ret = vu;
vu.ret = uv;
g[u].add(uv);
g[v].add(vu);
}
for (int i = 0; i < n; i++) {
if (g[i].size() > m) {
m = g[i].size();
s = i;
}
}
List<Road>[] ans = new List[m];
for (int i = 0; i < m; i++) {
ans[i] = new ArrayList<Road>();
}
dfs(s, -1, -1, g, ans);
println(m);
for (int i = 0; i < m; i++) {
print(ans[i].size());
for (Road road : ans[i]) {
print(' ');
print(road.id);
}
println();
}
}
void dfs(int u, int p, int b, List<Road>[] g, List<Road>[] ans) {
int color = 0;
for (Road road : g[u]) {
int v = road.dst;
if (v == p) {
continue;
}
if (color == b) {
++color;
}
ans[color].add(road);
dfs(v, u, color, g, ans);
++color;
}
}
void skip() {
while (hasNext()) {
next();
}
}
int[][] nextMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String next() {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
int[] nextArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException err) {
return null;
}
}
public C(OutputStream outputStream) {
super(outputStream);
}
static BufferedReader reader;
static StringTokenizer tokenizer = new StringTokenizer("");
static Random rnd = new Random();
static boolean OJ;
public static void main(String[] args) throws IOException {
OJ = System.getProperty("ONLINE_JUDGE") != null;
C solution = new C(System.out);
if (OJ) {
reader = new BufferedReader(new InputStreamReader(System.in));
solution.run();
} else {
reader = new BufferedReader(new FileReader(new File(C.class.getName() + ".txt")));
long timeout = System.currentTimeMillis();
while (solution.hasNext()) {
solution.run();
solution.println();
solution.println("----------------------------------");
}
solution.println("time: " + (System.currentTimeMillis() - timeout));
}
solution.close();
reader.close();
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.OptionalInt;
import java.util.ArrayList;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.Collection;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
private int n;
private City[] g;
public void solve(int testNumber, InputReader in, OutputWriter out) {
g = new City[n = in.readInt()];
for (int i = 0; i < n; i++) {
g[i] = new City();
}
for (int i = 0; i < n - 1; i++) {
int a = in.readInt() - 1;
int b = in.readInt() - 1;
g[a].out.add(new Edge(i + 1, g[b]));
g[b].out.add(new Edge(i + 1, g[a]));
}
g[0].color = -1;
dfs(g[0], null);
int daysCount = Arrays.stream(g).mapToInt(i -> i.out.size()).max().getAsInt();
IntList[] ans = new IntList[daysCount];
for (int i = 0; i < ans.length; i++) {
ans[i] = new IntArrayList();
}
for (int i = 0; i < n; i++) {
g[i].out.stream()
.filter(edge -> edge.color != -1)
.forEach(edge -> ans[edge.color].add(edge.index));
}
out.printLine(daysCount);
for (IntList day : ans) {
out.print(day.size());
day.sort();
for (int edge : day) {
out.print(" " + edge);
}
out.printLine();
}
}
private void dfs(City cur, City parent) {
int color = 0;
for (Edge edge : cur.out) {
if (edge.city == parent) {
continue;
}
if (color == cur.color) {
color++;
}
edge.city.color = color;
edge.color = color;
color++;
dfs(edge.city, cur);
}
}
}
static class Sorter {
private static final int INSERTION_THRESHOLD = 16;
private Sorter() {
}
public static void sort(IntList list, IntComparator comparator) {
quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1,
comparator);
}
private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) {
if (to - from < INSERTION_THRESHOLD) {
insertionSort(list, from, to, comparator);
return;
}
if (remaining == 0) {
heapSort(list, from, to, comparator);
return;
}
remaining--;
int pivotIndex = (from + to) >> 1;
int pivot = list.get(pivotIndex);
list.swap(pivotIndex, to);
int storeIndex = from;
int equalIndex = to;
for (int i = from; i < equalIndex; i++) {
int value = comparator.compare(list.get(i), pivot);
if (value < 0) {
list.swap(storeIndex++, i);
} else if (value == 0) {
list.swap(--equalIndex, i--);
}
}
quickSort(list, from, storeIndex - 1, remaining, comparator);
for (int i = equalIndex; i <= to; i++) {
list.swap(storeIndex++, i);
}
quickSort(list, storeIndex, to, remaining, comparator);
}
private static void heapSort(IntList list, int from, int to, IntComparator comparator) {
for (int i = (to + from - 1) >> 1; i >= from; i--) {
siftDown(list, i, to, comparator, from);
}
for (int i = to; i > from; i--) {
list.swap(from, i);
siftDown(list, from, i - 1, comparator, from);
}
}
private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) {
int value = list.get(start);
while (true) {
int child = ((start - delta) << 1) + 1 + delta;
if (child > end) {
return;
}
int childValue = list.get(child);
if (child + 1 <= end) {
int otherValue = list.get(child + 1);
if (comparator.compare(otherValue, childValue) > 0) {
child++;
childValue = otherValue;
}
}
if (comparator.compare(value, childValue) >= 0) {
return;
}
list.swap(start, child);
start = child;
}
}
private static void insertionSort(IntList list, int from, int to, IntComparator comparator) {
for (int i = from + 1; i <= to; i++) {
int value = list.get(i);
for (int j = i - 1; j >= from; j--) {
if (comparator.compare(list.get(j), value) <= 0) {
break;
}
list.swap(j, j + 1);
}
}
}
}
static interface IntReversableCollection extends IntCollection {
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static interface IntList extends IntReversableCollection {
public abstract int get(int index);
public abstract void set(int index, int value);
public abstract void addAt(int index, int value);
public abstract void removeAt(int index);
default public void swap(int first, int second) {
if (first == second) {
return;
}
int temp = get(first);
set(first, get(second));
set(second, temp);
}
default public IntIterator intIterator() {
return new IntIterator() {
private int at;
private boolean removed;
public int value() {
if (removed) {
throw new IllegalStateException();
}
return get(at);
}
public boolean advance() {
at++;
removed = false;
return isValid();
}
public boolean isValid() {
return !removed && at < size();
}
public void remove() {
removeAt(at);
at--;
removed = true;
}
};
}
default public void add(int value) {
addAt(size(), value);
}
default public IntList sort() {
sort(IntComparator.DEFAULT);
return this;
}
default public IntList sort(IntComparator comparator) {
Sorter.sort(this, comparator);
return this;
}
}
static interface IntCollection extends net_egork_generated_collections_IntStream {
public int size();
default public void add(int value) {
throw new UnsupportedOperationException();
}
default public IntCollection addAll(net_egork_generated_collections_IntStream values) {
for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) {
add(it.value());
}
return this;
}
}
static class City {
public final List<Edge> out = new ArrayList<>();
public int color;
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine() {
writer.println();
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
public void printLine(int i) {
writer.println(i);
}
}
static class IntArrayList extends IntAbstractStream implements IntList {
private int size;
private int[] data;
public IntArrayList() {
this(3);
}
public IntArrayList(int capacity) {
data = new int[capacity];
}
public IntArrayList(IntCollection c) {
this(c.size());
addAll(c);
}
public IntArrayList(net_egork_generated_collections_IntStream c) {
this();
if (c instanceof IntCollection) {
ensureCapacity(((IntCollection) c).size());
}
addAll(c);
}
public IntArrayList(IntArrayList c) {
size = c.size();
data = c.data.clone();
}
public IntArrayList(int[] arr) {
size = arr.length;
data = arr.clone();
}
public int size() {
return size;
}
public int get(int at) {
if (at >= size) {
throw new IndexOutOfBoundsException("at = " + at + ", size = " + size);
}
return data[at];
}
private void ensureCapacity(int capacity) {
if (data.length >= capacity) {
return;
}
capacity = Math.max(2 * data.length, capacity);
data = Arrays.copyOf(data, capacity);
}
public void addAt(int index, int value) {
ensureCapacity(size + 1);
if (index > size || index < 0) {
throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
}
if (index != size) {
System.arraycopy(data, index, data, index + 1, size - index);
}
data[index] = value;
size++;
}
public void removeAt(int index) {
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
}
if (index != size - 1) {
System.arraycopy(data, index + 1, data, index, size - index - 1);
}
size--;
}
public void set(int index, int value) {
if (index >= size) {
throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
}
data[index] = value;
}
}
static interface IntIterator {
public int value() throws NoSuchElementException;
public boolean advance();
public boolean isValid();
}
static abstract class IntAbstractStream implements net_egork_generated_collections_IntStream {
public String toString() {
StringBuilder builder = new StringBuilder();
boolean first = true;
for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
if (first) {
first = false;
} else {
builder.append(' ');
}
builder.append(it.value());
}
return builder.toString();
}
public boolean equals(Object o) {
if (!(o instanceof net_egork_generated_collections_IntStream)) {
return false;
}
net_egork_generated_collections_IntStream c = (net_egork_generated_collections_IntStream) o;
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while (it.isValid() && jt.isValid()) {
if (it.value() != jt.value()) {
return false;
}
it.advance();
jt.advance();
}
return !it.isValid() && !jt.isValid();
}
public int hashCode() {
int result = 0;
for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
result *= 31;
result += it.value();
}
return result;
}
}
static interface IntComparator {
public static final IntComparator DEFAULT = (first, second) -> {
if (first < second) {
return -1;
}
if (first > second) {
return 1;
}
return 0;
};
public int compare(int first, int second);
}
static class Edge {
public final int index;
public final City city;
public int color = -1;
public Edge(int index, City city) {
this.index = index;
this.city = city;
}
}
static interface net_egork_generated_collections_IntStream
extends Iterable<Integer>, Comparable<net_egork_generated_collections_IntStream> {
public IntIterator intIterator();
default public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private IntIterator it = intIterator();
public boolean hasNext() {
return it.isValid();
}
public Integer next() {
int result = it.value();
it.advance();
return result;
}
};
}
default public int compareTo(net_egork_generated_collections_IntStream c) {
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while (it.isValid() && jt.isValid()) {
int i = it.value();
int j = jt.value();
if (i < j) {
return -1;
} else if (i > j) {
return 1;
}
it.advance();
jt.advance();
}
if (it.isValid()) {
return 1;
}
if (jt.isValid()) {
return -1;
}
return 0;
}
}
}
| JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> G[200005];
vector<int> ans[200005];
void dfs(int n, int p, int dayp) {
int k = 1;
for (auto z : G[n]) {
if (z.first != p) {
if (k == dayp) k++;
ans[k].push_back(z.second);
dfs(z.first, n, k);
k++;
}
}
}
int main() {
int n, i, j, t;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (i = 0; i < n - 1; i++) {
cin >> j >> t;
G[j].push_back({t, i + 1});
G[t].push_back({j, i + 1});
}
dfs(1, -1, -1);
int day;
for (day = 1; day < 200005; day++)
if (ans[day].empty()) break;
day--;
cout << day << '\n';
for (i = 1; i < day + 1; i++) {
cout << ans[i].size() << " ";
for (auto z : ans[i]) cout << z << " ";
cout << '\n';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2 * 100 * 1000 + 2;
int n, k;
vector<pair<int, int>> adj[N];
vector<int> ans[N];
void dfs(int u, int par, int d) {
int cnt = 0;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first, num = adj[u][i].second;
if (v == par) continue;
cnt++;
if (cnt == d) cnt++;
ans[cnt].push_back(num);
k = max(k, cnt);
dfs(v, u, cnt);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
dfs(1, 0, 0);
cout << k << endl;
for (int i = 1; i <= k; i++) {
cout << ans[i].size() << ' ';
for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' ';
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.util.*;
public class tests {
Fs scn = new Fs(System.in);
int n = scn.nextInt();
ArrayList<Pair>[] graph = new ArrayList[n];
boolean[] used = new boolean[n];
ArrayList<Pair2> pars = new ArrayList<>();
PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
new tests().solve();
}
public tests() throws IOException {
}
class Pair {
int j;
int num;
Pair(int g, int n) {
j = g;
num = n;
}
}
class Pair2 {
int i;
int j;
int p;
Pair2(int a, int b, int c) {
i = a;
j = b;
p = c;
}
}
public void solve() throws IOException {
for (int c = 0; c < n; c++) {
graph[c] = new ArrayList<>();
}
for (int c = 0; c < n - 1; c++) {
int i = scn.nextInt() - 1;
int j = scn.nextInt() - 1;
graph[i].add(new Pair(j, c));
graph[j].add(new Pair(i, c));
}
int s = 0;
//System.out.println("dfs");
//dfs(s);
//System.out.println("bfs");
bfs(s);
}
void dfs(int v) {
Stack<Integer> s = new Stack<>();
ArrayList<Integer>[] days = new ArrayList[n - 1];
for (int i = 0; i < n - 1; i++) {
days[i] = new ArrayList<>();
}
int k = 0;
used[v] = true;
s.push(v);
int cur;
while (!s.isEmpty()) {
cur = s.pop();
for (Pair u : graph[cur]) {
if (!used[u.j]) {
used[u.j] = true;
pars.add(new Pair2(cur, u.j, u.num));
System.out.println(cur + 1 + " " + (u.j + 1));
/*while (ats[d].contains(cur) || ats[d].contains(u.j)){
d++;
}
//System.out.println(d + " " + cur + " " + u.j);
ats[d].add(cur);
ats[d].add(u.j);
days[d].add(u.num);
k = Math.max(k,d);*/
s.push(u.j);
}
}
}
/*boolean[] u = new boolean[pars.size()];
int d = 0;
TreeSet<Integer> hs;
for (int i = 0; i < pars.size(); i++)
if (!u[i]) {
hs = new TreeSet<>();
days[d].add(pars.get(i).p);
hs.add(pars.get(i).i);
hs.add(pars.get(i).j);
u[i] = true;
for (int j = i + 1; j < pars.size(); j++)
if (!u[j] && !hs.contains(pars.get(j).i) && !hs.contains(pars.get(j).j)) {
days[d].add(pars.get(j).p);
hs.add(pars.get(j).i);
hs.add(pars.get(j).j);
u[j] = true;
}
d++;
}
System.out.println(d);
for (int i = 0; i < d; i++) {
System.out.print(days[i].size());
for (int j = 0; j < days[i].size(); j++) {
System.out.print(" " + (days[i].get(j) + 1));
}
System.out.println();
}*/
}
void bfs(int v) {
Arrays.fill(used, false);
Queue<Integer> q = new ArrayDeque<>();
ArrayList<Integer>[] days = new ArrayList[n - 1];
for (int i = 0; i < n - 1; i++) {
days[i] = new ArrayList<>();
}
used[v] = true;
q.add(v);
int[] h = new int[n];
Arrays.fill(h, -1);
//h[0] = 0;
int cur;
//int c = 0;
int d = 0;
int k = 0;
while (!q.isEmpty()) {
cur = q.remove();
d = 0;
for (Pair u : graph[cur]) {
if (!used[u.j]) {
used[u.j] = true;
q.add(u.j);
if(d == h[cur]) d++;
h[u.j] = d;
days[d].add(u.num);
//System.out.println(d + " " + (cur+1) + " " + (u.j+1));
//System.out.println("c " + c + "p " + p);
k = Math.max(k,d);
d++;
}
}
//p = c;
}
out.println(k+1);
for (int i = 0; i < k+1; i++) {
out.print(days[i].size());
for (int j = 0; j < days[i].size(); j++) {
out.print(" " + (days[i].get(j) + 1));
}
out.println();
}
out.flush();
out.close();
}
class Fs {
BufferedReader bf;
StringTokenizer st;
Fs(InputStream in) {
bf = new BufferedReader(new InputStreamReader(in));
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
private String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(bf.readLine());
return st.nextToken();
}
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 9e5 + 6;
int ans[N];
vector<pair<int, int> > G[N];
vector<int> aa[N];
int n;
void dfs(int x, int f, int c) {
int st = 1;
for (auto k : G[x]) {
if (k.first != f) {
if (st == c) st++;
ans[k.second] = st;
dfs(k.first, x, st);
++st;
}
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(make_pair(y, i));
G[y].push_back(make_pair(x, i));
}
dfs(1, 0, 0);
int z = 0;
for (int i = 1; i <= n - 1; i++) {
if (ans[i] == 0) continue;
z = max(z, ans[i]);
aa[ans[i]].push_back(i);
}
for (int i = 1; i <= n; i++) {
}
cout << z << endl;
for (int i = 1; i <= n + n + n; i++) {
if (aa[i].size() == 0) {
continue;
}
cout << aa[i].size() << " ";
for (auto k : aa[i]) {
cout << k << " ";
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
vector<pair<int, int> > adj[200005];
int n, a, b, tot;
void dfs(int pos, int par, int used) {
int start = 0, baby, path, j;
for (int i = 0; i < adj[pos].size(); i++) {
baby = adj[pos][i].first;
path = adj[pos][i].second;
if (baby != par) {
j = start;
if (j != used) {
v[j].push_back(path);
dfs(baby, pos, j);
start = j + 1;
tot = max(tot, j + 1);
} else {
dfs(baby, pos, j + 1);
v[j + 1].push_back(path);
tot = max(j + 2, tot);
start = j + 2;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
dfs(1, 0, -1);
cout << tot << "\n";
for (int i = 0; i < tot; i++) {
cout << v[i].size() << " ";
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << "\n";
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
vector<int> g[MAX];
vector<int> ans[MAX];
int rootId = 0;
int rootPow = 0;
map<pair<int, int>, int> m;
int bad[MAX];
bool used[MAX];
void dfs(int v, int parent) {
if (used[v]) {
return;
}
used[v] = true;
int sz = (g[v].size());
int p = bad[v];
int time = 0;
for (int i = 0; i < sz; i++) {
if (g[v][i] == parent) {
continue;
}
if (time == p) time++;
int next = g[v][i];
int id = m[make_pair(v, next)];
ans[time].push_back(id);
bad[next] = time;
dfs(g[v][i], v);
time++;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
m[make_pair(x, y)] = i;
m[make_pair(y, x)] = i;
if (g[x].size() > rootPow) {
rootPow = g[x].size();
rootId = x;
}
if (g[y].size() > rootPow) {
rootPow = g[y].size();
rootId = y;
}
}
bad[rootId] = -1;
dfs(rootId, -1);
printf("%d\n", rootPow);
for (int i = 0; i < n; i++) {
if (ans[i].size() > 0) {
printf("%d", ans[i].size());
int sz = ans[i].size();
for (int j = 0; j < sz; j++) {
printf(" %d", ans[i][j] + 1);
}
printf("\n");
}
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int MOD = 1000000007;
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
//
// Big numbers arithmetic bugs:
// int overflow
// sorting, or taking max, after MOD
void solve() throws IOException {
int n = ri();
List<List<int[]>> adj = new ArrayList<>(n);
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int i = 0; i < n-1; i++) {
int[] uv = ril(2);
adj.get(uv[0]-1).add(new int[]{uv[1]-1, i});
adj.get(uv[1]-1).add(new int[]{uv[0]-1, i});
}
dfs(adj, 0, -1, -1);
Map<Integer, List<Integer>> cToE = new HashMap<>();
for (int i = 0; i < n-1; i++) {
int c = eToC.get(i);
if (!cToE.containsKey(c)) cToE.put(c, new ArrayList<>());
cToE.get(c).add(i);
}
pw.println(cToE.size());
for (int c : cToE.keySet()) {
pw.print(cToE.get(c).size());
for (int e : cToE.get(c)) {
pw.print(" " + (e+1));
}
pw.println();
}
}
Map<Integer, Integer> eToC = new HashMap<>();
void dfs(List<List<int[]>> adj, int u, int p, int banned) {
int c = 0;
for (int[] v : adj.get(u)) {
if (v[0] == p) continue;
if (c == banned) c++;
eToC.put(v[1], c);
dfs(adj, v[0], u, c);
c++;
}
}
// Template code below
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine().trim());
}
long rl() throws IOException {
return Long.parseLong(br.readLine().trim());
}
int[] ril(int n) throws IOException {
int[] nums = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
long[] rll(int n) throws IOException {
long[] nums = new long[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
long x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
int[] rkil() throws IOException {
int sign = 1;
int c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return ril(x);
}
long[] rkll() throws IOException {
int sign = 1;
int c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return rll(x);
}
char[] rs() throws IOException {
return br.readLine().toCharArray();
}
void sort(int[] A) {
Random r = new Random();
for (int i = A.length-1; i > 0; i--) {
int j = r.nextInt(i+1);
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
Arrays.sort(A);
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
vector<pair<int, int> > g[maxn];
vector<int> solution[maxn];
int n;
void dfs(int node, int p, int opentime) {
int br = 1;
for (auto i : g[node]) {
if (i.first != p) {
if (br == opentime) br++;
solution[br].push_back(i.second);
dfs(i.first, node, br);
br++;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
g[x].push_back(make_pair(y, i));
g[y].push_back(make_pair(x, i));
}
dfs(1, 0, 0);
int result = 0;
for (int i = 1; i <= n; i++) {
if (solution[i].size()) result = i;
}
cout << result << "\n";
for (int i = 1; i <= result; i++) {
cout << solution[i].size() << " ";
for (int j : solution[i]) cout << j << " ";
cout << "\n";
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class CF {
public static void main(String[] args) {
InputReader inputReader = new InputReader(System.in);
PrintWriter printWriter = new PrintWriter(System.out, true);
int n = inputReader.nextInt();
List<Edge>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
int u = inputReader.nextInt() - 1;
int v = inputReader.nextInt() - 1;
adj[u].add(new Edge(v, i + 1));
adj[v].add(new Edge(u, i + 1));
}
int maxDegree = -1;
for (int i = 0; i < n; i++) {
int nextSize = adj[i].size();
maxDegree = maxDegree > nextSize ? maxDegree : nextSize;
}
ArrayList<Integer>[] schedule = new ArrayList[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
schedule[i] = new ArrayList<>();
}
int[] excludeDay = new int[n];
int[] nextFreeDay = new int[n];
boolean[] isVisited = new boolean[n];
ArrayDeque<Integer> queue = new ArrayDeque<>();
isVisited[0] = true;
queue.offer(0);
while (!queue.isEmpty()) {
int u = queue.poll();
for (Edge e : adj[u]) {
int v = e.getV();
if (!isVisited[v]) {
if (++nextFreeDay[u] != excludeDay[u]) {
excludeDay[v] = nextFreeDay[u];
} else {
excludeDay[v] = ++nextFreeDay[u];
}
schedule[nextFreeDay[u]].add(e.getId());
isVisited[v] = true;
queue.offer(v);
}
}
}
printWriter.println(maxDegree);
for (int i = 1; i <= maxDegree; i++) {
printWriter.print(schedule[i].size() + " ");
for (Integer road : schedule[i]) {
printWriter.print(road + " ");
}
printWriter.println();
}
printWriter.close();
}
private static class Edge {
private int v;
private int id;
public Edge(int v, int id) {
this.v = v;
this.id = id;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
private static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public int[] nextIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; ++i) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; ++i) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; ++i) {
array[i] = nextDouble();
}
return array;
}
public String[] nextStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; ++i) {
array[i] = next();
}
return array;
}
public boolean[][] nextBooleanTable(int rows, int columns, char trueCharacter) {
boolean[][] table = new boolean[rows][columns];
for (int i = 0; i < rows; ++i) {
String row = next();
assert row.length() == columns;
for (int j = 0; j < columns; ++j) {
table[i][j] = (row.charAt(j) == trueCharacter);
}
}
return table;
}
public char[][] nextCharTable(int rows, int columns) {
char[][] table = new char[rows][];
for (int i = 0; i < rows; ++i) {
table[i] = next().toCharArray();
assert table[i].length == columns;
}
return table;
}
public int[][] nextIntTable(int rows, int columns) {
int[][] table = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextInt();
}
}
return table;
}
public long[][] nextLongTable(int rows, int columns) {
long[][] table = new long[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextLong();
}
}
return table;
}
public double[][] nextDoubleTable(int rows, int columns) {
double[][] table = new double[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
table[i][j] = nextDouble();
}
}
return table;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = readLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(readLine());
}
return tokenizer.nextToken();
}
public String readLine() {
String line;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n;
vector<pair<int, int>> g[maxn];
vector<int> ans[maxn];
int tot;
void dfs(int u, int fa, int ti) {
int cur = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].first;
if (v == fa) continue;
if (++cur == ti) cur++;
tot = max(tot, cur);
ans[cur].push_back(g[u][i].second);
dfs(v, u, cur);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(make_pair(y, i));
g[y].push_back(make_pair(x, i));
}
tot = 0;
dfs(1, -1, 0);
cout << tot << endl;
for (int i = 1; i <= tot; i++) {
printf("%d ", ans[i].size());
for (int j = 0; j < ans[i].size(); j++)
printf("%d%c", ans[i][j], j == ans[i].size() - 1 ? '\n' : ' ');
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
vector<int> res[200009];
vector<pair<int, int> > path[200009];
int last[200009];
queue<pair<int, int> > Q;
int main() {
cin >> n;
int a, b;
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
path[a].push_back({b, i});
path[b].push_back({a, i});
}
a = 0;
int poi = 0;
for (int i = 1; i <= n; i++) {
int sz = path[i].size();
if (sz > poi) {
poi = sz;
a = i;
}
}
Q.push({a, 0});
while (Q.size()) {
a = Q.front().first;
b = Q.front().second;
Q.pop();
int sz = path[a].size();
for (int i = 0; i < sz; i++) {
int aa = path[a][i].first;
int bb = path[a][i].second;
if (aa != b) {
res[last[a]].push_back(bb);
last[a]++;
last[a] %= poi;
last[aa] = last[a];
Q.push({aa, a});
}
}
}
printf("%d\n", poi);
for (int i = 0; i < poi; i++) {
int sz = res[i].size();
printf("%d ", sz);
for (int j = 0; j < sz; j++) printf("%d ", res[i][j]);
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<pair<long long, long long> > adj[N];
long long deg[N];
vector<long long> ans[N];
long long anss[N];
void dfs(long long u, long long p, long long prev) {
long long curr = 1;
for (auto k : adj[u]) {
if (k.first == p) continue;
if (curr == prev) curr++;
anss[k.second] = curr;
dfs(k.first, u, curr);
curr++;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
deg[u]++;
deg[v]++;
}
long long top = 1;
for (long long i = 1; i < n + 1; i++)
if (deg[i] > deg[top]) top = i;
dfs(top, -1, 0);
cout << deg[top] << "\n";
for (long long i = 0; i < n - 1; i++) ans[anss[i]].push_back(i + 1);
for (long long i = 1; i < deg[top] + 1; i++) {
cout << ans[i].size() << " ";
for (auto k : ans[i]) cout << k << " ";
cout << "\n";
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int gcd1(int a, int b) {
if (a == 0) return b;
return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
long long ans = 1LL, val = base;
while (ex > 0LL) {
if (ex & 1LL) ans = (ans * val) % 1000000007LL;
val = (val * val) % 1000000007LL;
ex = ex >> 1LL;
}
return ans;
}
const int maxn = 2e5 + 10;
vector<int> adj[maxn], v[maxn];
bool visit[maxn];
int n, x, y, parent[maxn], a[maxn], ans;
map<pair<int, int>, int> mp;
void dfs(int start, int par, int ind) {
visit[start] = true;
parent[start] = par, a[start] = ind;
int cnt = 1;
for (int i = 0; i < adj[start].size(); i++) {
int pt = adj[start][i];
if (!visit[pt]) {
if (cnt == a[start]) {
dfs(pt, start, ++cnt);
cnt++;
} else {
dfs(pt, start, cnt);
cnt++;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
mp[make_pair(x, y)] = mp[make_pair(y, x)] = i;
ans = max(ans, (int)adj[x].size());
ans = max(ans, (int)adj[y].size());
}
dfs(1, 0, 0);
for (int i = 2; i <= n; i++) {
int par = parent[i];
v[a[i]].push_back(mp[make_pair(i, par)]);
}
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
cout << v[i].size() << " ";
for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 * 1000 + 20;
bool mark[MAXN];
int n, x, y, g;
vector<pair<int, int>> gr[MAXN];
vector<int> ans[MAXN];
void dfs(int v, int u, int g) {
int h = 1;
for (pair<int, int> i : gr[v]) {
if (i.first != u) {
if (h == g) h++;
ans[h].push_back(i.second);
dfs(i.first, v, h);
h++;
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> x >> y;
gr[x].push_back({y, i});
gr[y].push_back({x, i});
g = max(g, max((int)gr[x].size(), (int)gr[y].size()));
}
cout << g << endl;
dfs(1, 0, 0);
for (int i = 1; i <= g; i++) {
cout << ans[i].size() << " ";
for (int j : ans[i]) cout << j << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, id;
Edge(int _u, int _id) {
u = _u;
id = _id;
}
};
vector<Edge> to[200005];
vector<int> ans[200005];
int skp[200005];
void dfs(int v, int p) {
int s = 1;
for (auto x : to[v]) {
if (x.u == p) continue;
s += (skp[v] == s);
ans[s].push_back(x.id);
skp[x.u] = s;
s++;
dfs(x.u, v);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
to[v].push_back(Edge(u, i));
to[u].push_back(Edge(v, i));
}
int root = 1;
for (int i = 2; i <= n; i++)
if (to[root].size() < to[i].size()) root = i;
cout << to[root].size() << "\n";
dfs(root, 0);
for (int i = 1; i <= to[root].size(); i++) {
cout << ans[i].size() << ' ';
for (auto x : ans[i]) cout << x << ' ';
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<vector<pair<int, int>>> res;
void dfs(int node, int parent, int day_parent) {
int cnt = 0;
for (auto neighbor : g[node]) {
if (neighbor == parent) continue;
if (cnt == day_parent) cnt++;
if (cnt >= res.size()) res.push_back(vector<pair<int, int>>());
res[cnt].push_back({min(node, neighbor), max(node, neighbor)});
dfs(neighbor, node, cnt);
cnt++;
}
}
void run() {
int n;
cin >> n;
g = vector<vector<int>>(n + 1);
map<pair<int, int>, int> m;
int nb_road = 1;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
m[{min(u, v), max(u, v)}] = nb_road++;
}
dfs(1, -1, -1);
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i].size() << " ";
for (int j = 0; j < res[i].size(); j++) {
cout << m[res[i][j]];
if (j != res[i].size()) cout << " ";
}
if (i != res[i].size() - 1) cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
run();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > vec;
vector<vector<int> > ans;
void w(int v, int u, int k) {
int k2 = 1;
for (int i = 0; i < vec[v].size(); ++i) {
if (k2 == k) {
++k2;
}
if (vec[v][i].first == u) {
continue;
}
ans[k2].push_back(vec[v][i].second);
w(vec[v][i].first, v, k2);
++k2;
}
}
int main() {
int n;
cin >> n;
vec.resize(n + 1);
ans.resize(n + 1);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
vec[a].push_back({b, i});
vec[b].push_back({a, i});
}
w(1, -1, 0);
int z = 0;
for (int i = 0; i < ans.size(); ++i) {
if (ans[i].size() != 0) {
++z;
}
}
cout << z << endl;
for (int i = 0; i < ans.size(); ++i) {
if (ans[i].size() != 0) {
cout << ans[i].size() << ' ';
for (int j = 0; j < ans[i].size(); ++j) {
cout << ans[i][j] + 1 << ' ';
}
cout << endl;
}
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > gr[200005];
vector<int> ans[200005];
int mx;
void dfs(int v, int pr, int day) {
int cd = 0;
for (auto &i : gr[v]) {
if (i.first == pr) continue;
while (++cd == day)
;
mx = max(mx, cd);
ans[cd].push_back(i.second);
dfs(i.first, v, cd);
}
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
gr[a].push_back({b, i + 1});
gr[b].push_back({a, i + 1});
}
dfs(1, -1, 0);
cout << mx;
for (int i = 1; i <= mx; i++) {
cout << '\n' << ans[i].size();
for (auto j : ans[i]) {
cout << ' ' << j;
}
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5;
struct edge {
int to, id;
};
vector<edge> g[maxn];
vector<int> res[maxn];
void dfs(int v, int p, int f) {
int c = 0;
for (auto &e : g[v]) {
if (e.to == p) {
continue;
}
if (c == f) {
++c;
}
res[c].push_back(e.id);
dfs(e.to, v, c);
++c;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i + 1 < n; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
dfs(0, -1, -1);
int c = 0;
for (int i = 0; i < n; ++i) {
c = max(c, (int)g[i].size());
}
cout << c << '\n';
for (int i = 0; i < c; ++i) {
cout << res[i].size() << ' ';
for (auto &e : res[i]) {
cout << e + 1 << ' ';
}
cout << '\n';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> g[n + 1];
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
vector<vector<int>> ans(n + 1);
int sum = 0;
function<void(int, int, int)> dfs = [&](int u, int f, int d) {
int t = 0;
for (auto x : g[u]) {
int v = x.first;
int id = x.second;
if (v != f) {
++t;
if (t == d) {
++t;
}
sum = max(sum, t);
ans[t].push_back(id);
dfs(v, u, t);
}
}
};
dfs(1, 0, 0);
cout << sum << '\n';
for (int i = 1; i <= sum; ++i) {
cout << (int)ans[i].size() << ' ';
for (auto j : ans[i]) {
cout << j << ' ';
}
cout << '\n';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, res;
bool visited[N];
vector<pair<int, int> > adj[N];
vector<int> output[N];
void Input() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(make_pair(v, i));
adj[v].push_back(make_pair(u, i));
}
}
void DFS(int u, int days_u_busy) {
visited[u] = true;
int days = 0;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first, edge = adj[u][i].second;
if (!visited[v]) {
days++;
if (days == days_u_busy) days++;
output[days].push_back(edge);
DFS(v, days);
}
}
res = max(res, days);
}
int main() {
Input();
DFS(1, -1);
cout << res << '\n';
for (int i = 1; i <= res; i++) {
cout << output[i].size() << " ";
for (int j = 0; j < output[i].size(); j++) cout << output[i][j] << " ";
cout << '\n';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, fh = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') fh = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * fh;
}
const int maxn = 2e5 + 5;
struct Edge {
int to, next, num;
} e[maxn << 1];
int head[maxn], tot, de[maxn], n, ans, root;
vector<int> g[maxn];
void connect(int x, int y, int i) {
e[++tot] = (Edge){y, head[x], i};
head[x] = tot;
}
void dfs(int now, int from, int last) {
int pos = 0;
for (int i = head[now]; i; i = e[i].next) {
int p = e[i].to;
if (p == from) continue;
++pos;
if (pos == last) ++pos;
g[pos].push_back(e[i].num);
dfs(p, now, pos);
}
}
int main() {
n = read();
for (register int i = 1; i <= n - 1; ++i) {
int x = read(), y = read();
connect(x, y, i);
connect(y, x, i);
de[x]++;
de[y]++;
}
for (register int i = 1; i <= n; ++i)
if (ans < de[i]) {
ans = de[i];
root = i;
}
dfs(root, 0, 0);
cout << ans << endl;
for (register int i = 1; i <= ans; ++i) {
int q = g[i].size();
printf("%d ", q);
for (register int j = 0; j <= q - 1; ++j) printf("%d ", g[i][j]);
puts("");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
int n;
vector<int> v[maxn];
vector<pair<int, int> > g[maxn];
set<int> s;
int color[maxn];
int ans;
void dfs(int v, int par = 0, int c = n) {
s.erase(c);
for (auto u : g[v])
if (u.first != par) {
color[u.second] = *s.begin();
s.erase(s.begin());
}
for (auto u : g[v])
if (u.first != par) {
s.insert(color[u.second]);
}
s.insert(c);
for (auto u : g[v])
if (u.first != par) {
dfs(u.first, v, color[u.second]);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int v, u, i = 0; i < n - 1; i++) {
cin >> v >> u;
v--, u--;
g[v].push_back({u, i});
g[u].push_back({v, i});
}
for (int i = 0; i < n; i++) ans = max(ans, (int)g[i].size());
for (int i = 0; i < n; i++) s.insert(i);
dfs(0);
for (int i = 0; i < n - 1; i++) v[color[i]].push_back(i);
cout << ans << endl;
for (int i = 0; i < ans; i++) {
cout << v[i].size() << " ";
for (auto c : v[i]) cout << c + 1 << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 * 2;
vector<pair<int, int>> gr[MAXN];
vector<int> ans[MAXN];
int check[MAXN];
void dfs(int, int);
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
gr[u].push_back({v, i});
gr[v].push_back({u, i});
}
int k = 0;
for (int i = 1; i <= n; i++) {
int temp = gr[i].size();
k = max(k, temp);
}
dfs(1, 11111111);
cout << k << endl;
for (int i = 1; i <= k; i++) {
cout << ans[i].size() << ' ';
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << ' ';
}
cout << endl;
}
return 0;
}
void dfs(int v, int lasd) {
int day = 1;
check[v] = 1;
for (pair<int, int> u : gr[v]) {
if (check[u.first] == false) {
if (day == lasd) day++;
dfs(u.first, day);
ans[day].push_back(u.second);
day++;
}
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2000 * 100 + 10;
int n, d;
vector<int> ans[N];
vector<pair<int, int>> adj[N];
void dfs(int root, int par = -1, int prv = 0) {
int nxt = 1;
for (auto u : adj[root])
if (u.first ^ par) {
if (nxt == prv) nxt++;
ans[nxt].push_back(u.second);
d = max(d, nxt);
dfs(u.first, root, nxt);
nxt++;
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1, v, u; i < n; i++) {
cin >> v >> u;
adj[v].push_back({u, i});
adj[u].push_back({v, i});
}
dfs(1);
cout << d << '\n';
for (int i = 1; i <= d; i++, cout << '\n') {
cout << (int)(ans[i].size()) << ' ';
for (auto x : ans[i]) cout << x << ' ';
}
cout << '\n';
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
int f[maxn], color[maxn];
vector<pair<int, int> > g[maxn];
vector<int> vmark[maxn];
int n;
void input() {
int z = 1;
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x, z));
z++;
}
}
void dfs(int v = 1, int p = 0) {
int mx = 1;
f[v] = p;
for (int i = 0; i < g[v].size(); i++)
if (g[v][i].first != p) {
if (mx == color[v]) mx++;
color[g[v][i].first] = mx;
vmark[mx].push_back(g[v][i].second);
mx++;
dfs(g[v][i].first, v);
}
}
int main() {
int mx = 0;
input();
dfs();
for (int i = 1; i < maxn; i++) {
if (vmark[i].size() == 0) break;
mx++;
}
cout << mx << endl;
for (int i = 1; i < maxn; i++) {
if (vmark[i].size() == 0) break;
cout << vmark[i].size() << " ";
for (int j = 0; j < vmark[i].size(); j++) cout << vmark[i][j] << " ";
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new Main().run(in, out);
out.close();
}
// should be the max(indeg) ?
int N;
List<Edge>[] adj;
List<Integer>[] day;
int maxDay;
void run(FastScanner in, PrintWriter out) {
N = in.nextInt();
adj = new List[N+1];
day = new List[N+1];
maxDay = 0;
for (int i = 0; i <= N; i++) {
adj[i] = new ArrayList<>();
day[i] = new ArrayList<>();
}
for (int i = 1; i < N; i++) {
int u = in.nextInt();
int v = in.nextInt();
Edge e = new Edge(u, v, i);
adj[u].add(e);
adj[v].add(e);
}
dfs(1, -1, 0);
out.println(maxDay);
for (int i = 1; i <= maxDay; i++) {
out.print(day[i].size());
for (int num : day[i]) out.print(" " + num);
out.println();
}
}
void dfs(int u, int p, int currDay) {
int blacklistDay = currDay;
currDay = 1;
if (currDay == blacklistDay) currDay++;
for (Edge e : adj[u]) {
int v = e.V(u);
if (v == p) continue;
day[currDay].add(e.idx);
maxDay = Math.max(currDay, maxDay);
dfs(v, u, currDay);
currDay++;
if (currDay == blacklistDay) currDay++;
}
}
class Edge {
int u;
int v;
int idx;
Edge(int u, int v, int idx) {
this.u = u;
this.v = v;
this.idx = idx;
}
int V(int i) {
return (i == u) ? v : u;
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int INF = 1e9 + 5;
long long int mod = 998244353;
long long int nax = 2e5;
vector<vector<long long int>> g(nax + 1), days(nax + 1);
map<pair<long long int, long long int>, long long int> mp;
void dfs(long long int node, long long int pa, long long int k,
long long int no) {
long long int x = 1;
for (auto child : g[node]) {
if (x == no) {
x++;
}
if (child != pa) {
days[x].push_back(mp[{child, node}]);
dfs(child, node, k, x++);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int n;
cin >> n;
for (long long int i = 0; i < n - 1; i++) {
long long int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
mp[{u, v}] = mp[{v, u}] = i + 1;
}
long long int k = 0;
for (long long int node = 1; node <= n; ++node) {
k = max(k, (long long int)g[node].size());
}
dfs(1, 0, k, 0);
cout << k << "\n";
for (long long int i = 1; i <= k; i++) {
cout << days[i].size() << " ";
for (auto ele : days[i]) {
cout << ele << " ";
}
cout << "\n";
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct edge {
int to, id;
};
int n, a, b, r;
vector<edge> g[200009];
vector<int> ret[200009];
void dfs(int pos, int pre, int pid) {
int curid = 0;
for (edge e : g[pos]) {
if (e.to != pre) {
if (pid == curid) curid++;
ret[curid].push_back(e.id);
dfs(e.to, pos, curid);
curid++;
}
}
r = max(r, curid);
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b;
a--, b--;
g[a].push_back(edge{b, i});
g[b].push_back(edge{a, i});
}
dfs(0, -1, -1);
cout << r << endl;
for (int i = 0; i < r; i++) {
cout << ret[i].size();
for (int j : ret[i]) cout << ' ' << j;
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
void dfs(int cur, int parent, int incr, ArrayList<Integer>[] adj, ArrayList<IntIntPair>[] res) {
int count = 0;
for(int nxt : adj[cur])
if(nxt != parent) {
if(count == incr) count++;
// System.out.println(cur + " " + parent + " " + incr + " " + count + " " + nxt);
res[count].add(new IntIntPair(cur, nxt));
dfs(nxt, cur, count, adj, res);
count++;
}
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[] as = new int[n - 1], bs = new int[n - 1];
IOUtils.readIntArrays(in, as, bs);
MiscUtils.decreaseByOne(as, bs);
TreeMap<IntIntPair, Integer> tm = new TreeMap<>();
for(int i = 0; i < n - 1; i++) {
tm.put(new IntIntPair(as[i], bs[i]), i + 1);
tm.put(new IntIntPair(bs[i], as[i]), i + 1);
}
ArrayList<Integer>[] adj = new ArrayList[n];
for(int i = 0; i < adj.length; i++)
adj[i] = new ArrayList<>();
for(int i = 0; i < n - 1; i++) {
adj[as[i]].add(bs[i]);
adj[bs[i]].add(as[i]);
}
int[] degree = new int[n];
for(int i = 0; i < n - 1; i++) {
degree[as[i]]++;
degree[bs[i]]++;
}
int maxdegree = ArrayUtils.maxElement(degree);
out.printLine(maxdegree);
ArrayList<IntIntPair>[] res = new ArrayList[maxdegree];
for(int i = 0; i < res.length; i++)
res[i] = new ArrayList<>();
dfs(0, -1, -1, adj, res);
for(ArrayList<IntIntPair> al : res) {
out.print(al.size() + " ");
for(IntIntPair p : al) {
out.print((tm.get(p)) + " ");
}
out.printLine();
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if(numChars == -1)
throw new InputMismatchException();
if(curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch(IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if(c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while(!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if(filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class ArrayUtils {
public static int maxElement(int[] array) {
return new IntArray(array).max();
}
}
static class IntArray extends IntAbstractStream implements IntList {
private int[] data;
public IntArray(int[] arr) {
data = arr;
}
public int size() {
return data.length;
}
public int get(int at) {
return data[at];
}
public void removeAt(int index) {
throw new UnsupportedOperationException();
}
}
static interface IntIterator {
public int value() throws NoSuchElementException;
public boolean advance();
public boolean isValid();
}
static interface IntReversableCollection extends IntCollection {
}
static interface IntCollection extends IntStream {
public int size();
}
static interface IntList extends IntReversableCollection {
public abstract int get(int index);
public abstract void removeAt(int index);
default public IntIterator intIterator() {
return new IntIterator() {
private int at;
private boolean removed;
public int value() {
if(removed) {
throw new IllegalStateException();
}
return get(at);
}
public boolean advance() {
at++;
removed = false;
return isValid();
}
public boolean isValid() {
return !removed && at < size();
}
public void remove() {
removeAt(at);
at--;
removed = true;
}
};
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for(int i = 0; i < objects.length; i++) {
if(i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine() {
writer.println();
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class MiscUtils {
public static void decreaseByOne(int[]... arrays) {
for(int[] array : arrays) {
for(int i = 0; i < array.length; i++)
array[i]--;
}
}
}
static interface IntStream extends Iterable<Integer>, Comparable<IntStream> {
public IntIterator intIterator();
default public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private IntIterator it = intIterator();
public boolean hasNext() {
return it.isValid();
}
public Integer next() {
int result = it.value();
it.advance();
return result;
}
};
}
default public int compareTo(IntStream c) {
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while(it.isValid() && jt.isValid()) {
int i = it.value();
int j = jt.value();
if(i < j) {
return -1;
} else if(i > j) {
return 1;
}
it.advance();
jt.advance();
}
if(it.isValid()) {
return 1;
}
if(jt.isValid()) {
return -1;
}
return 0;
}
default public int max() {
int result = Integer.MIN_VALUE;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
int current = it.value();
if(current > result) {
result = current;
}
}
return result;
}
}
static abstract class IntAbstractStream implements IntStream {
public String toString() {
StringBuilder builder = new StringBuilder();
boolean first = true;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
if(first) {
first = false;
} else {
builder.append(' ');
}
builder.append(it.value());
}
return builder.toString();
}
public boolean equals(Object o) {
if(!(o instanceof IntStream)) {
return false;
}
IntStream c = (IntStream) o;
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while(it.isValid() && jt.isValid()) {
if(it.value() != jt.value()) {
return false;
}
it.advance();
jt.advance();
}
return !it.isValid() && !jt.isValid();
}
public int hashCode() {
int result = 0;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
result *= 31;
result += it.value();
}
return result;
}
}
static class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for(int i = 0; i < arrays[0].length; i++) {
for(int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = Integer.hashCode(first);
result = 31 * result + Integer.hashCode(second);
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
@SuppressWarnings({"unchecked"})
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if(value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
}
| JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, ans;
vector<int> v[200010], v_ans[200010];
map<pair<int, int>, int> m;
void dfs(int node, int par, int col) {
ans = max(ans, col);
int c = 1;
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
if (c == col) c++;
dfs(v[node][i], node, c);
v_ans[c++].push_back(m[make_pair(node, v[node][i])]);
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y), v[y].push_back(x);
m[make_pair(x, y)] = i, m[make_pair(y, x)] = i;
}
dfs(1, 0, 0);
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
cout << v_ans[i].size();
for (int j = 0; j < v_ans[i].size(); j++) {
cout << ' ' << v_ans[i][j];
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200009;
vector<pair<int, int> > g[N];
bool used[N];
vector<int> steps[N];
int u[N], v[N];
deque<int> q;
int ind[N];
bool usedTo[N];
int main() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &u[i], &v[i]);
g[u[i]].push_back(make_pair(v[i], i));
g[v[i]].push_back(make_pair(u[i], i));
}
steps[1].push_back(1);
q.push_back(1);
ind[1] = 1;
while (!q.empty()) {
int i = q.front();
q.pop_front();
int now = ind[i];
used[i] = 1;
if (!usedTo[u[i]])
for (int j = 0, c = 1; j < (int)g[u[i]].size(); ++j) {
int V = g[u[i]][j].second;
if (!used[V]) {
if (c == now) c++;
ind[V] = c;
q.push_back(V);
used[V] = 1;
steps[c].push_back(V);
c++;
}
}
if (!usedTo[v[i]])
for (int j = 0, c = 1; j < (int)g[v[i]].size(); ++j) {
int V = g[v[i]][j].second;
if (!used[V]) {
if (c == now) c++;
ind[V] = c;
q.push_back(V);
used[V] = 1;
steps[c].push_back(V);
c++;
}
}
usedTo[u[i]] = 1;
usedTo[v[i]] = 1;
}
int k = 1;
for (int i = 1; i <= n; ++i) {
if (!steps[i].empty()) {
k = i;
}
}
cout << k << "\n";
for (int i = 1; i <= k; ++i) {
printf("%d ", (int)steps[i].size());
for (int j = 0; j < (int)steps[i].size(); ++j) printf("%d ", steps[i][j]);
printf("\n");
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> G[200005];
vector<int> work[200005];
int mxday;
void dfs(int now, int pa, int pud) {
int nt = 1;
for (auto i : G[now]) {
if (i.first == pa) continue;
if (nt == pud) ++nt;
work[nt].push_back(i.second);
mxday = max(mxday, nt);
dfs(i.first, now, nt);
++nt;
}
}
int main() {
int n;
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
G[u].emplace_back(v, i);
G[v].emplace_back(u, i);
}
dfs(1, 1, 0);
cout << mxday << '\n';
for (int i = 1; i <= mxday; ++i) {
cout << work[i].size();
for (int j : work[i]) cout << " " << j;
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:67108864")
using namespace std;
int n;
vector<int> vc[222222];
vector<int> cn[222222];
set<int, less<int> > vv[222222];
int dr[222222][2];
bool color[222222];
int mx = 0;
void dfs(int v) {
color[v] = true;
for (size_t i = 0; i < cn[v].size(); i++) {
int j = cn[v][i];
int k = 0;
if (dr[j][0] == v) {
k = dr[j][1];
} else {
k = dr[j][0];
}
if (!color[k]) {
int l = *(vv[v].begin());
vv[v].erase(vv[v].begin());
if (l > mx) mx = l;
set<int, less<int> >::iterator ii = vv[k].find(l);
if (ii != vv[k].end()) vv[k].erase(ii);
vc[l].push_back(j);
dfs(k);
}
}
}
int main() {
scanf("%i", &n);
memset(color, 0, sizeof(color));
for (int i = 0; i < n - 1; i++) {
scanf("%i%i", &dr[i][0], &dr[i][1]);
dr[i][0]--;
dr[i][1]--;
vv[dr[i][0]].insert(vv[dr[i][0]].size());
vv[dr[i][1]].insert(vv[dr[i][1]].size());
cn[dr[i][0]].push_back(i);
cn[dr[i][1]].push_back(i);
}
dfs(0);
printf("%i\n", mx + 1);
for (int i = 0; i <= mx; i++) {
printf("%i ", vc[i].size());
for (int j = 0; j < vc[i].size(); j++) {
printf("%i ", vc[i][j] + 1);
}
printf("\n");
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void bfs(vector<vector<uint64_t>>& g,
map<tuple<uint64_t, uint64_t>, uint64_t>& i2r) {
vector<bool> visit(g.size());
deque<tuple<uint64_t, uint64_t, uint64_t>> ev;
map<int64_t, vector<uint64_t>> roads_by_day;
visit[1] = true;
int day = 0;
for (auto e : g[1]) {
day += 1;
ev.push_back({1, e, day});
roads_by_day[day].push_back(i2r[{1, e}]);
}
while (!ev.empty()) {
auto crt = ev.front();
uint64_t nv = get<1>(crt);
ev.pop_front();
if (!visit[nv]) {
visit[nv] = true;
int day = 0;
int noDay = get<2>(crt);
for (auto e : g[nv]) {
if (!visit[e]) {
day += 1;
if (day == noDay) {
day += 1;
}
ev.push_back({nv, e, day});
roads_by_day[day].push_back(i2r[{min(nv, e), max(nv, e)}]);
}
}
}
}
cout << roads_by_day.size() << endl;
for (auto kv : roads_by_day) {
cout << kv.second.size() << " ";
for (auto ed : kv.second) {
cout << ed << " ";
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
uint64_t n;
cin >> n;
vector<vector<uint64_t>> v(n + 1);
map<tuple<uint64_t, uint64_t>, uint64_t> i2r;
for (uint64_t i = 0; i < n - 1; i++) {
uint64_t x;
cin >> x;
uint64_t y;
cin >> y;
v[x].push_back(y);
v[y].push_back(x);
i2r[{min(x, y), max(x, y)}] = i + 1;
}
bfs(v, i2r);
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int, int> > g[200055];
vector<int> res[200055];
void dfs(int id, int par, int pre) {
int cnt = 0;
for (int i = 0; i < (int)g[id].size(); i++) {
int to = g[id][i].first, u = g[id][i].second;
if (to == par) continue;
cnt++;
if (cnt == pre) cnt++;
res[cnt].push_back(u);
dfs(to, id, cnt);
k = max(k, cnt);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
g[x].push_back(pair<int, int>(y, i));
g[y].push_back(pair<int, int>(x, i));
}
dfs(1, -1, 0);
cout << k << '\n';
for (int i = 1; i <= k; i++) {
cout << res[i].size();
for (int j = 0; j < (int)res[i].size(); j++) {
cout << " " << res[i][j] + 1;
}
cout << '\n';
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[200001];
bool visit[200001];
const int mod = 1000000007;
long long mod_mult(long long a, long long b) {
return ((a % mod) * (b % mod)) % mod;
}
long long mult(long long x, long long y) {
long long prod = 1;
while (y > 0) {
if (y & 1) prod = mod_mult(prod, x);
x = mod_mult(x, x);
y /= 2;
}
return prod;
}
vector<int> ans[200005];
int m = 0;
void dfs(int cur, int org) {
visit[cur] = true;
int c = 0;
for (long long i = 0; i < adj[cur].size(); i += 1) {
if (!visit[adj[cur][i].first]) {
if (c == org) c++;
ans[c].push_back(adj[cur][i].second);
dfs(adj[cur][i].first, c);
c++;
}
}
m = max(m, c);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, i, t, a, b;
cin >> n;
for (int i = 0; i < n - 1; i++)
cin >> a >> b, adj[a].push_back({b, i + 1}), adj[b].push_back({a, i + 1});
dfs(1, -1);
cout << m << endl;
for (int i = 0; i < m; i++) {
cout << ans[i].size() << " ";
for (auto it : ans[i]) cout << it << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int read() {
int ans = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return ans * f;
}
const int N = 2e5 + 5;
int n, du[N], head[N], tot, ans;
vector<int> agg[N];
struct Edge {
int v, ne;
} e[N * 2];
inline void add(int u, int v);
inline void dfs(int u, int fa, int last);
int main() {
n = read();
for (int i = 1; i < n; ++i) {
int u = read(), v = read();
du[u]++, du[v]++;
add(u, v);
add(v, u);
ans = max(ans, max(du[u], du[v]));
}
printf("%d\n", ans);
dfs(1, -1, 0);
for (int i = 1; i <= ans; ++i) {
printf("%d ", agg[i].size());
for (int j = 0; j < agg[i].size(); ++j) printf("%d ", agg[i][j]);
printf("\n");
}
return 0;
}
inline void add(int u, int v) {
e[++tot] = (Edge){v, head[u]};
head[u] = tot;
}
inline void dfs(int u, int fa, int last) {
int j = 1;
for (int i = head[u]; i; i = e[i].ne) {
int v = e[i].v;
if (v == fa) continue;
if (j == last) j++;
agg[j].push_back((i + 1) / 2);
dfs(v, u, j);
++j;
}
return;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
vector<pair<int, int>> v[200001];
vector<int> ans[200000];
int k = 0;
void dfs(int cur, int pre, int tm) {
int fixtime = 0;
for (auto i : v[cur]) {
int nxt = i.first;
if (nxt == pre) continue;
++fixtime;
if (fixtime == tm) ++fixtime;
if (fixtime > k) k = fixtime;
ans[fixtime].push_back(i.second);
dfs(nxt, cur, fixtime);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, w;
cin >> u >> w;
v[u].push_back({w, i});
v[w].push_back({u, i});
}
dfs(1, 0, 0);
cout << k << endl;
for (int i = 1; i <= k; ++i) {
cout << ans[i].size() << " ";
for (int d : ans[i]) cout << d << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int MAXM = MAXN - 1;
int n;
int deg[MAXN];
int ghead[MAXN], gprv[2 * MAXM], gnxt[2 * MAXM], gto[2 * MAXM];
int ihead[MAXN], iprv[2 * MAXM], inxt[2 * MAXM];
int qhead, qprv[MAXN], qnxt[MAXN], qcopy[MAXN], nqcopy;
int mark[MAXN], curmark;
int rhead[MAXM + 1], rcnt[MAXM + 1], rnxt[MAXM];
void remdedge(int idx, int* head, int* prv, int* nxt) {
if (head[gto[idx ^ 1]] == idx) head[gto[idx ^ 1]] = nxt[idx];
if (prv[idx] != -1) nxt[prv[idx]] = nxt[idx];
if (nxt[idx] != -1) prv[nxt[idx]] = prv[idx];
prv[idx] = nxt[idx] = -1;
}
void remnode(int idx) {
if (qhead == idx) qhead = qnxt[idx];
if (qprv[idx] != -1) qnxt[qprv[idx]] = qnxt[idx];
if (qnxt[idx] != -1) qprv[qnxt[idx]] = qprv[idx];
qprv[idx] = qnxt[idx] = -1;
}
void repair(int idx) {
rnxt[idx] = rhead[curmark];
rhead[curmark] = idx;
++rcnt[curmark];
int a = gto[2 * idx + 1], b = gto[2 * idx + 0];
mark[a] = mark[b] = curmark;
remdedge(2 * idx + 0, ghead, gprv, gnxt);
remdedge(2 * idx + 1, ghead, gprv, gnxt);
remdedge(2 * idx + 0, ihead, iprv, inxt);
remdedge(2 * idx + 1, ihead, iprv, inxt);
if (--deg[a] == 1) remdedge(ghead[a] ^ 1, ihead, iprv, inxt), remnode(a);
if (--deg[b] == 1) remdedge(ghead[b] ^ 1, ihead, iprv, inxt), remnode(b);
}
void process(int at) {
int idx = -1;
for (int x = ghead[at]; x != -1; x = gnxt[x])
if (mark[gto[x]] != curmark) {
idx = x >> 1;
break;
}
if (idx == -1) {
printf("error: curmark=%d, cnt=%d, at=%d\n", curmark, rcnt[curmark], at);
exit(0);
}
repair(idx);
for (int x = ihead[gto[2 * idx + 0]]; x != -1;) {
int y = x;
x = inxt[x];
if (mark[gto[y]] != curmark) process(gto[y]);
}
for (int x = ihead[gto[2 * idx + 1]]; x != -1;) {
int y = x;
x = inxt[x];
if (mark[gto[y]] != curmark) process(gto[y]);
}
}
void run() {
n = MAXN;
scanf("%d", &n);
for (int i = (0); i < (n); ++i) ghead[i] = -1, deg[i] = 0;
for (int i = (0); i < (n - 1); ++i) {
int a = 0, b = i + 1;
scanf("%d%d", &a, &b);
--a, --b;
gnxt[2 * i + 0] = ghead[a];
ghead[a] = 2 * i + 0;
gto[2 * i + 0] = b;
gprv[2 * i + 0] = -1;
if (gnxt[2 * i + 0] != -1) gprv[gnxt[2 * i + 0]] = 2 * i + 0;
++deg[a];
gnxt[2 * i + 1] = ghead[b];
ghead[b] = 2 * i + 1;
gto[2 * i + 1] = a;
gprv[2 * i + 1] = -1;
if (gnxt[2 * i + 1] != -1) gprv[gnxt[2 * i + 1]] = 2 * i + 1;
++deg[b];
}
for (int at = (0); at < (n); ++at) {
ihead[at] = -1;
for (int x = ghead[at]; x != -1; x = gnxt[x])
if (deg[gto[x]] >= 2) {
inxt[x] = ihead[at];
ihead[at] = x;
iprv[x] = -1;
if (inxt[x] != -1) iprv[inxt[x]] = x;
} else
iprv[x] = inxt[x] = -1;
}
qhead = -1;
for (int at = (0); at < (n); ++at)
if (deg[at] >= 2) {
qnxt[at] = qhead;
qhead = at;
qprv[at] = -1;
if (qnxt[at] != -1) qprv[qnxt[at]] = at;
} else
qprv[at] = qnxt[at] = -1;
for (int at = (0); at < (n - 1); ++at) rnxt[at] = -1;
memset(mark, -1, sizeof(mark));
curmark = -1;
while (true) {
++curmark;
rhead[curmark] = -1, rcnt[curmark] = 0;
nqcopy = 0;
for (int at = qhead; at != -1; at = qnxt[at]) qcopy[nqcopy++] = at;
if (nqcopy == 0)
for (int at = (0); at < (n); ++at)
if (deg[at] >= 1) qcopy[nqcopy++] = at;
if (nqcopy == 0) break;
for (int i = (0); i < (nqcopy); ++i) {
int at = qcopy[i];
if (mark[at] != curmark) process(at);
}
}
printf("%d\n", curmark);
for (int i = (0); i < (curmark); ++i) {
printf("%d", rcnt[i]);
fflush(stdout);
for (int x = rhead[i]; x != -1; x = rnxt[x])
printf(" %d", x + 1), fflush(stdout);
puts("");
fflush(stdout);
}
}
int main() {
run();
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
const double PI = acos(-1);
const long long INFF = (long long)1e18;
const int INF = (int)1e9;
const int mod = (int)1e9 + 7;
const int MAX = (int)2e5 + 7;
vector<pair<int, int> > edge[MAX];
vector<int> ans[MAX];
int ck[MAX];
void dfs(int u, int pa, int id) {
int p = 1;
for (auto x : edge[u]) {
if (x.first == pa) continue;
if (p == ck[id]) p++;
ans[p].push_back(x.second);
ck[x.second] = p;
dfs(x.first, u, x.second);
p++;
}
return;
}
int main(void) {
int n;
scanf("%d", &n);
;
for (int i = (1); i < (n); ++i) {
int u, v;
scanf("%d %d", &u, &v);
;
edge[u].push_back(make_pair(v, i));
edge[v].push_back(make_pair(u, i));
}
dfs(1, 0, 0);
int res = 0;
for (int i = (1); i < (n + 1); ++i) res = max(res, ((int)edge[i].size()));
printf("%d\n", res);
for (int i = (1); i < (res + 1); ++i) {
printf("%d", ((int)ans[i].size()));
for (auto x : ans[i]) printf(" %d", x);
puts("");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import sys
mx=2*10**5+10
g=[[] for _ in range(mx)]
days=[[] for _ in range(mx)]
maxi=-1
n=int(raw_input())
t=sys.stdin.read().split("\n")
for i in range(n-1):
l=t[i]
u,v=map(int,l.split())
g[u].append((v,i+1))
g[v].append((u,i+1))
q=[(1,1,-1) for i in range(n)]
h,t=0,1
while h<t:
u,pu,c=q[h][0],q[h][1],q[h][2]
h+=1
maxi=max(maxi,c);
color=0
for uv in g[u]:
v,euv=uv[0],uv[1]
if v!=pu:
if color==c: color+=1
days[color].append(euv)
q[t]=(v,u,color)
t+=1
color+=1
print(maxi+1)
for i in range(maxi+1):
print len(days[i]),' '.join(map(str,days[i]))
| PYTHON |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 200000;
vector<pair<int, int> > g[N];
int depth[N], pa[N];
void dfs(int u, int par, int d) {
pa[u] = par;
depth[u] = d;
for (pair<int, int>& i : g[u])
if (i.first != par) dfs(i.first, u, d + 1);
}
void solve() {
int n, u, v;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
u--;
v--;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(0, -1, 0);
for (int i = 1; i < n; i++)
for (int j = 0; j < g[i].size(); j++)
if (g[i][j].first == pa[i]) {
g[i].erase(g[i].begin() + j);
break;
}
int deg[n];
deg[0] = g[0].size();
for (int i = 1; i < n; i++) deg[i] = g[i].size() + 1;
int max_deg = deg[0];
for (int i = 1; i < n; i++) max_deg = max(max_deg, deg[i]);
set<pair<int, int> > ver[max_deg + 1];
ver[deg[0]].insert({depth[0], 0});
for (int i = 1; i < n; i++) ver[deg[i]].insert({depth[i], i});
cout << max_deg << "\n";
for (int i = max_deg; i > 0; i--) {
unordered_set<int> vis;
vector<int> ans;
for (auto j : ver[i]) {
deg[j.second]--;
if (vis.find(j.second) != vis.end()) continue;
v = g[j.second].back().first;
ans.push_back(g[j.second].back().second);
if (deg[v] == i)
vis.insert(v);
else {
ver[deg[v]].erase({depth[v], v});
deg[v]--;
ver[deg[v]].insert({depth[v], v});
}
g[j.second].pop_back();
}
cout << ans.size() << " ";
for (int& j : ans) cout << j << " ";
cout << "\n";
if (i == 1) return;
for (auto j : ver[i]) ver[i - 1].insert(j);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 25;
int n, u, v, res;
vector<pair<int, int> > G[N];
vector<int> ans[N];
void dfs(int u, int p, int last) {
res = max(res, (int)G[u].size());
int cnt = 0;
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i].first;
if (v == p) continue;
cnt++;
if (cnt == last) cnt++;
ans[cnt].push_back(G[u][i].second);
dfs(v, u, cnt);
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> u >> v;
G[u].push_back(pair<int, int>(v, i));
G[v].push_back(pair<int, int>(u, i));
}
dfs(1, 1, 0);
cout << res << '\n';
for (int i = 1; i <= res; ++i) {
cout << ans[i].size() << ' ';
for (int j = 0; j < ans[i].size(); ++j) cout << ans[i][j] << ' ';
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static MyScanner in;
static PrintWriter out;
// static Timer timer = new Timer();
public static void main(String[] args) throws IOException {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
int n = in.nextInt();
Graph g = new Graph(n);
for (int i = 1; i < n; i++)
g.add(in.nextInt() - 1, in.nextInt() - 1, i);
ArrayList<Integer>[] ans = g.dfs();
int size = 0;
for (int i = 0; i < ans.length; i++)
if (ans[i].size() != 0)
size++;
out.println(size);
for (int i = 0; i < ans.length; i++) {
if (ans[i].size() == 0)
continue;
out.print(ans[i].size());
for (int a : ans[i])
out.print(" " + a);
out.println();
}
}
}
class Graph {
private int n;
private ArrayList<Pair>[] g;
private ArrayList<Integer>[] ans;
public Graph(int n) {
this.n = n;
g = new ArrayList[n];
for (int i = 0; i < n; i++)
g[i] = new ArrayList<>();
}
public void add(int v, int w, int num) {
g[v].add(new Pair(w, num));
g[w].add(new Pair(v, num));
}
public ArrayList<Integer>[] dfs() {
ans = new ArrayList[n];
for (int i = 0; i < n; i++)
ans[i] = new ArrayList<>();
dfs(0, -1, -1);
return ans;
}
private void dfs(int pos, int forbid, int prev) {
int day = 1;
for (Pair p : g[pos]) {
if (p.v == prev)
continue;
if (day == forbid)
day++;
ans[day].add(p.num);
dfs(p.v, day, pos);
day++;
}
}
}
class Pair {
int v, num;
public Pair(int v, int num) {
this.v = v;
this.num = num;
}
}
class MyScanner {
private final BufferedReader br;
private StringTokenizer st;
private String last;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public MyScanner(String path) throws IOException {
br = new BufferedReader(new FileReader(path));
}
public MyScanner(String path, String decoder) throws IOException {
br = new BufferedReader(new InputStreamReader(new FileInputStream(path), decoder));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
last = null;
return st.nextToken();
}
String nextLine() throws IOException {
st = null;
return (last == null) ? br.readLine() : last;
}
boolean hasNext() {
if (st != null && st.hasMoreElements())
return true;
try {
while (st == null || !st.hasMoreElements()) {
last = br.readLine();
st = new StringTokenizer(last);
}
}
catch (Exception e) {
return false;
}
return true;
}
String[] nextStrings(int n) throws IOException {
String[] arr = new String[n];
for (int i = 0; i < n; i++)
arr[i] = next();
return arr;
}
String[] nextLines(int n) throws IOException {
String[] arr = new String[n];
for (int i = 0; i < n; i++)
arr[i] = nextLine();
return arr;
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
int[] nextInts(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
Integer[] nextIntegers(int n) throws IOException {
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] next2Ints(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = nextInt();
return arr;
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
long[] nextLongs(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
long[][] next2Longs(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = nextLong();
return arr;
}
double nextDouble() throws IOException {
return Double.parseDouble(next().replace(',', '.'));
}
double[] nextDoubles(int size) throws IOException {
double[] arr = new double[size];
for (int i = 0; i < size; i++)
arr[i] = nextDouble();
return arr;
}
boolean nextBool() throws IOException {
String s = next();
if (s.equalsIgnoreCase("true") || s.equals("1"))
return true;
if (s.equalsIgnoreCase("false") || s.equals("0"))
return false;
throw new IOException("Boolean expected, String found!");
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) throws IOException{
Scan sc = new Scan();
int n = sc.scanInt();
Solution sol = new Solution(n);
for(int i = 1; i < n; ++i){
sol.addEdge(i, sc.scanInt(), sc.scanInt());
}
sol.solve();
}
}
class Solution {
ArrayList<Edge>[] mp;
ArrayList<Integer>[] ans;
int output;
Solution(int n){
mp = new ArrayList[n + 1];
ans = new ArrayList[n + 1];
for(int i = 1; i <= n; ++i){
mp[i] = new ArrayList<Edge>();
ans[i] = new ArrayList<Integer>();
}
output = -1;
}
void Dfs(int u,int from,int prenum)
{
int day=0;
for(Edge e : mp[u])
{
int i = e.v;
if(i==from)continue;
day++;if(prenum==day)day++;
ans[day].add(e.id);
Dfs(i,u,day);
}
output=Math.max(output,day);
}
void addEdge(int i , int a, int b){
mp[a].add(new Edge(b, i));
mp[b].add(new Edge(a, i));
}
void solve() throws IOException{
Print pt = new Print();
Dfs(1, -1, 0);
pt.println(output);
for(int i=1;i<=output;i++)
{
pt.print(ans[i].size() + " ");
for(int j : ans[i])
{
pt.print(j + " ");
}
pt.print("\n");
}
pt.close();
}
}
class Edge {
int v, id;
Edge(int v, int id){
this.v = v;
this.id = id;
}
}
// fast I/O
class Scan
{
private byte[] buf=new byte[1024]; //Buffer of Bytes
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException //Scan method used to scan buf
{
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int big=0;
int n=scan();
while(isWhiteSpace(n)) //Removing starting whitespaces
n=scan();
int neg=1;
if(n=='-') //If Negative Sign encounters
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
big*=10;
big+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*big;
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
class Print
{
private final BufferedWriter bw;
public Print()
{
this.bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object)throws IOException
{
bw.append(""+object);
}
public void println(Object object)throws IOException
{
print(object);
bw.append("\n");
}
public void close()throws IOException
{
bw.close();
}
} | JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
void dfs(int cur, int parent, int incr, ArrayList<Integer>[] adj, ArrayList<IntIntPair>[] res) {
int count = 0;
for(int nxt : adj[cur])
if(nxt != parent) {
if(count == incr) count++;
res[count].add(new IntIntPair(cur, nxt));
dfs(nxt, cur, count, adj, res);
count++;
}
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[] as = new int[n - 1], bs = new int[n - 1];
IOUtils.readIntArrays(in, as, bs);
MiscUtils.decreaseByOne(as, bs);
HashMap<IntIntPair, Integer> tm = new HashMap<>();
for(int i = 0; i < n - 1; i++) {
tm.put(new IntIntPair(as[i], bs[i]), i + 1);
tm.put(new IntIntPair(bs[i], as[i]), i + 1);
}
ArrayList<Integer>[] adj = new ArrayList[n];
for(int i = 0; i < adj.length; i++)
adj[i] = new ArrayList<>();
for(int i = 0; i < n - 1; i++) {
adj[as[i]].add(bs[i]);
adj[bs[i]].add(as[i]);
}
int[] degree = new int[n];
for(int i = 0; i < n - 1; i++) {
degree[as[i]]++;
degree[bs[i]]++;
}
int maxdegree = ArrayUtils.maxElement(degree);
out.printLine(maxdegree);
ArrayList<IntIntPair>[] res = new ArrayList[maxdegree];
for(int i = 0; i < res.length; i++)
res[i] = new ArrayList<>();
dfs(0, -1, -1, adj, res);
for(ArrayList<IntIntPair> al : res) {
out.print(al.size() + " ");
for(IntIntPair p : al) {
out.print((tm.get(p)) + " ");
}
out.printLine();
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if(numChars == -1)
throw new InputMismatchException();
if(curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch(IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if(c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while(!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if(filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class ArrayUtils {
public static int maxElement(int[] array) {
return new IntArray(array).max();
}
}
static class IntArray extends IntAbstractStream implements IntList {
private int[] data;
public IntArray(int[] arr) {
data = arr;
}
public int size() {
return data.length;
}
public int get(int at) {
return data[at];
}
public void removeAt(int index) {
throw new UnsupportedOperationException();
}
}
static interface IntIterator {
public int value() throws NoSuchElementException;
public boolean advance();
public boolean isValid();
}
static interface IntReversableCollection extends IntCollection {
}
static interface IntCollection extends IntStream {
public int size();
}
static interface IntList extends IntReversableCollection {
public abstract int get(int index);
public abstract void removeAt(int index);
default public IntIterator intIterator() {
return new IntIterator() {
private int at;
private boolean removed;
public int value() {
if(removed) {
throw new IllegalStateException();
}
return get(at);
}
public boolean advance() {
at++;
removed = false;
return isValid();
}
public boolean isValid() {
return !removed && at < size();
}
public void remove() {
removeAt(at);
at--;
removed = true;
}
};
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for(int i = 0; i < objects.length; i++) {
if(i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine() {
writer.println();
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class MiscUtils {
public static void decreaseByOne(int[]... arrays) {
for(int[] array : arrays) {
for(int i = 0; i < array.length; i++)
array[i]--;
}
}
}
static interface IntStream extends Iterable<Integer>, Comparable<IntStream> {
public IntIterator intIterator();
default public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private IntIterator it = intIterator();
public boolean hasNext() {
return it.isValid();
}
public Integer next() {
int result = it.value();
it.advance();
return result;
}
};
}
default public int compareTo(IntStream c) {
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while(it.isValid() && jt.isValid()) {
int i = it.value();
int j = jt.value();
if(i < j) {
return -1;
} else if(i > j) {
return 1;
}
it.advance();
jt.advance();
}
if(it.isValid()) {
return 1;
}
if(jt.isValid()) {
return -1;
}
return 0;
}
default public int max() {
int result = Integer.MIN_VALUE;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
int current = it.value();
if(current > result) {
result = current;
}
}
return result;
}
}
static abstract class IntAbstractStream implements IntStream {
public String toString() {
StringBuilder builder = new StringBuilder();
boolean first = true;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
if(first) {
first = false;
} else {
builder.append(' ');
}
builder.append(it.value());
}
return builder.toString();
}
public boolean equals(Object o) {
if(!(o instanceof IntStream)) {
return false;
}
IntStream c = (IntStream) o;
IntIterator it = intIterator();
IntIterator jt = c.intIterator();
while(it.isValid() && jt.isValid()) {
if(it.value() != jt.value()) {
return false;
}
it.advance();
jt.advance();
}
return !it.isValid() && !jt.isValid();
}
public int hashCode() {
int result = 0;
for(IntIterator it = intIterator(); it.isValid(); it.advance()) {
result *= 31;
result += it.value();
}
return result;
}
}
static class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for(int i = 0; i < arrays[0].length; i++) {
for(int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = Integer.hashCode(first);
result = 31 * result + Integer.hashCode(second);
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
@SuppressWarnings({"unchecked"})
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if(value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
}
| JAVA |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2 * 100 * 1000 + 7;
int n, cou;
int arr_p[N], color[N], a[N];
vector<int> ans[N];
pair<int, int> road[N];
vector<int> gr[N];
int f(int);
void dfs(int);
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
road[i] = {x, y};
gr[x].push_back(y);
gr[y].push_back(x);
}
dfs(1);
for (int i = 1; i < n; i++) {
y = road[i].first;
if (arr_p[road[i].second] == y) y = road[i].second;
ans[color[y]].push_back(i);
}
cout << cou << endl;
for (int i = 1; i <= cou; i++) {
cout << ans[i].size();
for (int j = 0; j < ans[i].size(); j++) {
cout << " " << ans[i][j];
}
cout << endl;
}
}
int f(int x) {
int cnt = a[x];
if (a[x] >= color[x]) cnt++;
cou = max(cou, cnt);
return cnt;
}
void dfs(int x) {
for (int i = 0; i < gr[x].size(); i++) {
if (arr_p[x] != gr[x][i]) {
int t = f(x);
a[x]++;
a[gr[x][i]]++;
color[gr[x][i]] = t;
arr_p[gr[x][i]] = x;
dfs(gr[x][i]);
}
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int Max = 2e5;
vector<pair<int, int> > G[Max + 5];
vector<int> ans[Max + 5];
int n;
int sum;
void dfs(int x, int fa, int t) {
int k = 0;
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i].first == fa) continue;
k++;
if (k == t) k++;
dfs(G[x][i].first, x, k);
ans[k].push_back(G[x][i].second);
sum = max(sum, k);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(make_pair(y, i));
G[y].push_back(make_pair(x, i));
}
dfs(1, 0, 0);
printf("%d\n", sum);
for (int i = 1; i <= sum; i++) {
printf("%d ", ans[i].size());
for (int j = 0; j < ans[i].size(); j++) {
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
const int N = 1000001;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, int>> a[n + 1];
int ans = 0;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(make_pair(v, i + 1));
a[v].push_back(make_pair(u, i + 1));
ans = max({ans, (int)a[u].size(), (int)a[v].size()});
}
cout << ans << '\n';
int final[n], vis[n + 1];
memset(final, -1, sizeof final);
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
map<int, int> d;
for (auto [v, ind] : a[u]) {
d[final[ind]] = 1;
}
int day = 1;
for (auto [v, ind] : a[u]) {
if (final[ind] == -1) {
while (d[day]) {
++day;
}
final[ind] = day++;
}
if (vis[v] == 0) {
vis[v] = 1;
q.push(v);
}
}
}
vector<int> f1[ans + 1];
for (int i = 1; i < n; i++) {
f1[final[i]].push_back(i);
}
for (int i = 1; i < ans + 1; i++) {
cout << f1[i].size() << ' ';
for (int e : f1[i]) {
cout << e << ' ';
}
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int dp[N];
int n, a, b;
vector<pair<int, int> > g[N];
vector<int> ans[N];
void dfs(int v, int p = -1, int d = -1) {
int cnt = 0;
for (int(i) = (0); i < (g[v].size()); ++(i)) {
int to = g[v][i].first;
if (to == p) continue;
if (cnt == d) ++cnt;
ans[cnt].push_back(g[v][i].second);
dfs(to, v, cnt++);
}
}
int main() {
cin >> n;
for (int(i) = (1); i < (n); ++(i)) {
cin >> a >> b;
--a;
--b;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
dfs(0);
int cnt = 0;
while (ans[cnt].size()) ++cnt;
cout << cnt << endl;
for (int(i) = (0); i < (cnt); ++(i)) {
cout << ans[i].size() << " ";
for (int(j) = (0); j < (ans[i].size()); ++(j)) cout << ans[i][j] << " ";
cout << "\n";
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<pair<int, int> > G[maxn];
vector<int> res[maxn];
int cnt = 0;
void dfs(int v, int p, int x) {
int vl = 1;
if (vl == x) {
++vl;
}
for (int i = 0; i < (int)G[v].size(); ++i) {
int u = G[v][i].first;
if (u != p) {
res[vl].push_back(G[v][i].second);
dfs(u, v, vl);
++vl;
if (vl == x) {
++vl;
}
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int u, v, id;
scanf("%d%d", &u, &v);
--u;
--v;
id = i + 1;
G[u].push_back(make_pair(v, id));
G[v].push_back(make_pair(u, id));
cnt = max(cnt, (int)max(G[v].size(), G[u].size()));
}
dfs(0, 0, 0);
printf("%d\n", cnt);
for (int i = 1; i <= cnt; ++i) {
printf("%d ", res[i].size());
for (int j = 0; j < (int)res[i].size(); ++j) {
printf("%d ", res[i][j]);
}
printf("\n");
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 2e5 + 10;
struct node {
int next, v, num;
} e[maxn * 2];
int tot, head[maxn];
vector<int> ans[maxn];
int du[maxn];
void add(int x, int y, int num) {
e[++tot].v = y;
e[tot].next = head[x];
e[tot].num = num;
head[x] = tot;
}
void dfs(int u, int fa, int last) {
int j = 1;
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].v;
if (to == fa) continue;
if (j == last) ++j;
ans[j].push_back(e[i].num);
dfs(to, u, j);
j++;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y, i);
add(y, x, i);
du[x]++;
du[y]++;
}
int cnt = 0;
for (int i = 1; i <= n; i++) cnt = max(cnt, du[i]);
dfs(1, 0, 0);
cout << cnt << endl;
for (int i = 1; i <= cnt; i++) {
cout << ans[i].size() << " ";
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200010;
vector<pair<int, int> > g[N];
vector<int> r[N];
int d[N];
void dfs(int u, int fa, int x) {
int c = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].first;
if (v == fa) continue;
c++;
if (c == x) c++;
r[c].push_back(g[u][i].second);
dfs(v, u, c);
}
}
int main() {
int n, mx = 0, u, v;
cin >> n;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
d[u]++;
d[v]++;
g[u].push_back(pair<int, int>(v, i));
g[v].push_back(pair<int, int>(u, i));
mx = max(mx, d[u]);
mx = max(mx, d[v]);
}
dfs(1, 0, 0);
cout << mx << endl;
for (int i = 1; i <= mx; i++) {
printf("%d", r[i].size());
for (int j = 0; j < r[i].size(); j++) {
printf(" %d", r[i][j]);
}
printf("\n");
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
void quit();
using namespace std;
const long double PI = acos(-1);
const long double EPS = 1e-10;
double __t;
int n;
int d[200100];
vector<pair<int, int> > g[200100];
int day[200100];
vector<int> ans[200100];
void dfs(int v, int skip = -1) {
int cnt = 0;
for (auto &e : g[v]) {
if (day[e.second] != -1) continue;
if (cnt == skip) ++cnt;
day[e.second] = cnt;
dfs(e.first, cnt);
++cnt;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int v, w;
for (int i = 0; i < n - 1; ++i) {
cin >> v >> w;
--v;
--w;
g[v].emplace_back(w, i);
g[w].emplace_back(v, i);
d[v]++;
d[w]++;
}
int mx = d[0];
v = 0;
for (int i = 1; i < n; ++i)
if (d[i] > mx) {
mx = d[i];
v = i;
}
memset(day, 0xff, sizeof(day));
dfs(v);
int dayCnt = 0;
for (int i = 0; i < n - 1; ++i) {
dayCnt = max(day[i] + 1, dayCnt);
ans[day[i]].push_back(i + 1);
}
cout << dayCnt << '\n';
for (int i = 0; i < dayCnt; ++i) {
cout << ans[i].size();
for (int first : ans[i]) {
cout << ' ' << first;
}
cout << '\n';
}
quit();
}
void quit() { exit(0); }
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int gcd1(int a, int b) {
if (a == 0) return b;
return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
long long ans = 1LL, val = base;
while (ex > 0LL) {
if (ex & 1LL) ans = (ans * val) % 1000000007LL;
val = (val * val) % 1000000007LL;
ex = ex >> 1LL;
}
return ans;
}
const int maxn = 2e5 + 10;
vector<int> adj[maxn], v[maxn];
bool visit[maxn];
int n, x, y, parent[maxn], a[maxn], ans;
map<pair<int, int>, int> mp;
void dfs(int start, int par, int ind) {
visit[start] = true;
parent[start] = par, a[start] = ind;
int cnt = 1;
for (int i = 0; i < adj[start].size(); i++) {
int pt = adj[start][i];
if (!visit[pt]) {
if (cnt == a[start]) {
dfs(pt, start, ++cnt);
cnt++;
} else {
dfs(pt, start, cnt);
cnt++;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
mp[make_pair(x, y)] = mp[make_pair(y, x)] = i;
ans = max(ans, (int)adj[x].size());
ans = max(ans, (int)adj[y].size());
}
dfs(1, 0, 0);
for (int i = 2; i <= n; i++) {
int par = parent[i];
v[a[i]].push_back(mp[make_pair(i, par)]);
}
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
cout << v[i].size() << " ";
for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << " ";
cout << endl;
}
return 0;
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > graph;
vector<vector<int> > ans;
int maxDeg;
int root;
void dfs(int u, int p, int day) {
int add = 1;
for (int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i].first;
int id = graph[u][i].second;
if (v == p) continue;
int nday = (day + add) % maxDeg;
add++;
ans[nday].push_back(id);
dfs(v, u, nday);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
graph.resize(n + 1);
for (int e = 1; e < n; e++) {
int u, v;
cin >> u >> v;
graph[u].emplace_back(v, e);
graph[v].emplace_back(u, e);
}
for (int i = 1; i <= n; i++) {
if (graph[i].size() > maxDeg) maxDeg = graph[i].size(), root = i;
}
cout << maxDeg << '\n';
ans.resize(maxDeg + 1);
dfs(root, 0, 0);
for (int i = 0; i < maxDeg; i++) {
cout << ans[i].size() << " ";
for (int x : ans[i]) cout << x << " ";
cout << '\n';
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<int> > edge(200005, vector<int>());
vector<vector<int> > ans(200005, vector<int>());
vector<bool> visited(200005, false);
map<pair<int, int>, int> mp;
void dfs(int i, int num) {
visited[i] = true;
int it = 1;
for (auto ed : edge[i]) {
if (!visited[ed]) {
if (it == num) {
it++;
}
ans[it].push_back(mp[{i, ed}]);
dfs(ed, it);
it++;
}
}
}
int main() {
int n;
cin >> n;
vector<int> vec(n, 0);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
int mini = min(a - 1, b - 1);
int maxi = max(a - 1, b - 1);
edge[mini].push_back(maxi);
edge[maxi].push_back(mini);
mp.insert({{mini, maxi}, i + 1});
mp.insert({{maxi, mini}, i + 1});
vec[a - 1]++;
vec[b - 1]++;
}
dfs(0, 0);
int res = 0;
for (int i = 0; i < n; i++) res = max(res, vec[i]);
cout << res << endl;
for (int i = 0; i < n; i++) {
if (ans[i].size() > 0) cout << ans[i].size() << " ";
for (auto u : ans[i]) {
cout << u << " ";
}
if (ans[i].size() > 0) cout << endl;
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, in[200005], day[200005];
vector<int> ans[200005];
vector<pair<int, int> > adj[200005];
void dfs(int now, int pre) {
map<int, int> M;
for (int i = 0; i < adj[now].size(); i++) {
int road = adj[now][i].second;
if (day[road]) {
M[day[road]] = 1e9;
}
}
int date = 1;
for (int i = 0; i < adj[now].size(); i++) {
int road = adj[now][i].second;
if (day[road] == 0) {
while (M[date] > 1) ++date;
M[date] = 1e9;
day[road] = date;
}
}
for (int i = 0; i < adj[now].size(); i++) {
int next = adj[now][i].first;
if (next != pre) dfs(next, now);
}
}
int main() {
scanf("%d", &n);
for (int a, b, i = 1; i <= n - 1; i++) {
scanf("%d %d", &a, &b);
adj[a].push_back(make_pair(b, i));
adj[b].push_back(make_pair(a, i));
++in[a];
++in[b];
}
int maxi = 0, V = 0;
for (int i = 1; i <= n; i++) {
if (maxi < in[i]) {
maxi = in[i];
V = i;
}
}
dfs(V, -1);
cout << maxi << endl;
for (int i = 1; i <= n - 1; i++) {
ans[day[i]].push_back(i);
}
for (int i = 1; i <= maxi; i++) {
cout << (int)ans[i].size();
for (int j = 0; j < ans[i].size(); j++) printf(" %d", ans[i][j]);
puts("");
}
}
| CPP |
638_C. Road Improvement | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
Input
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.
Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
Output
First print number k β the minimum number of days needed to repair all the roads in Berland.
In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them.
Examples
Input
4
1 2
3 4
3 2
Output
2
2 2 1
1 3
Input
6
3 4
5 4
3 2
1 3
4 6
Output
3
1 1
2 2 3
2 4 5
Note
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
vector<pair<int, int> > adj[maxn];
vector<int> v[maxn];
bool visit[maxn];
int n, m, u, v1, ans;
long long modx(long long base, long long ex) {
long long ans = 1LL, val = base;
while (ex > 0LL) {
if (ex & 1LL) ans = (ans * val) % 1000000007LL;
val = (val * val) % 1000000007LL;
ex = ex >> 1LL;
}
return ans;
}
void dfs(int start, int day) {
visit[start] = true;
int cnt = 1;
for (int i = 0; i < adj[start].size(); i++) {
int node = adj[start][i].first;
if (!visit[node]) {
if (cnt == day) cnt++;
ans = max(ans, cnt);
v[cnt].push_back(adj[start][i].second);
dfs(node, cnt++);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v1;
adj[u].push_back(make_pair(v1, i));
adj[v1].push_back(make_pair(u, i));
}
dfs(1, 0);
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
cout << v[i].size();
for (int j = 0; j < v[i].size(); j++) cout << " " << v[i][j];
cout << endl;
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.