#include <cstdio>
#include <iostream>
#include <cassert>
#include <vector>
#include <cstdlib>
#include <string>
#include <utility>
#include <tuple>
using namespace std;
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v);
int count_common_roads(const std::vector<int>& r);
bool isSubtask1(int n) {return n<=7;}
bool isSubtask2(int n) {return n<=50;}
bool isSubtask3(int n) {return n<=240;}
void dfs3(int startNode, int startEdge, int parent, int now, vector<vector<pair<int,int>>>& edgeL,
vector<int>& r2, vector<int>& r2Idx, vector<int>& dNode, vector<vector<int>>& dEdge) {
dNode[now]=1;
for(auto& elem: edgeL[now]) {
int nextNode,edgeID; tie(nextNode,edgeID) = elem;
if (nextNode==parent) continue;
if (nextNode==startNode) {dEdge[startEdge].push_back(edgeID); continue;}
if (!dNode[nextNode]) {
r2.push_back(edgeID);
dfs3(startNode,startEdge,now,nextNode,edgeL,r2,r2Idx,dNode,dEdge);
}
}
return;
}
vector<int> subtask3(int n, std::vector<int>& u, std::vector<int>& v) {
int m = u.size();
vector<int> r;
vector<int> treeEdge(m);
vector<vector<pair<int,int>>> edgeL(n);
for(int i=0; i<m; i++) {
edgeL[u[i]].push_back(make_pair(v[i],i));
edgeL[v[i]].push_back(make_pair(u[i],i));
}
// printf("Edge list\n");
// for(int i=0; i<n; i++) {
// printf("%d: ", i); for(auto& elem: edgeL[i]) printf("(%d,%d) ", get<0>(elem),get<1>(elem)); printf("\n");
// }
// printf("\n");
for(int i=0; i<n; i++) {
vector<int> dNode(n); vector<vector<int>> dEdge(m);
dNode[i] = 1;
vector<int> r2, r2Idx;
int nextNode,edgeID;
for(auto& elem: edgeL[i]) {
tie(nextNode,edgeID) = elem;
if (dNode[nextNode]) continue;
r2.push_back(edgeID);
r2Idx.push_back(r2.size()-1);
dfs3(i,edgeID,i,nextNode,edgeL,r2,r2Idx,dNode,dEdge);
// printf("i=%d: dEdge[%d]: ",i,edgeID); for(int elem: dEdge[edgeID]) printf("%d ",elem); printf("\n");
// printf("i=%d: r2: ",i); for(auto& elem: r2) printf("%d ", elem); printf("\n");
// printf("i=%d: r2Idx: ",i); for(auto& elem: r2Idx) printf("%d ", elem); printf("\n");
}
// printf("i=%d: dEdge\n",i);
// for(int j=0; j<dEdge.size(); j++) {printf("%d: ",j); for(int elem: dEdge[j]) printf("%d ",elem); printf("\n");}
// return r;
for(int j=0; j<r2Idx.size(); j++) {
vector<int> result;
// printf("i=%d: r2: ",i); for(auto& elem: r2) printf("%d ", elem); printf("\n");
int maxValue = count_common_roads(r2);
// printf("maxValue = %d ",maxValue);
result.push_back(maxValue);
int nextEdge = r2[r2Idx[j]];
for(int elem: dEdge[nextEdge]) {
r2[r2Idx[j]] = elem;
int temp = count_common_roads(r2);
// printf("temp=%d ",temp);
maxValue = max(maxValue,temp);
result.push_back(temp);
}
// printf("\nresult: ");for(int elem: result) printf("%d ",elem); printf("\n");
if(result[0]==maxValue) {
// printf("treeEdge[%d]=1\n",nextEdge);
treeEdge[nextEdge]=1;}
for(int j=1; j<result.size(); j++)
if (result[j] == maxValue) {
// printf("treeEdge[%d]=1\n",dEdge[nextEdge][j-1]);
treeEdge[dEdge[nextEdge][j-1]]=1;}
r2[r2Idx[j]] = nextEdge;
// printf("\ntreeEdge: "); for(int j=0; j<treeEdge.size(); j++) printf("%d ",treeEdge[j]); printf("\n");
}
}
for(int i=0; i<m; i++) if (treeEdge[i]) r.push_back(i);
// printf("r: ");
// for(int elem: r) printf("%d ",elem); printf("\n");
return r;
}
void dfs(int parent, int now, std::vector<int>& visit, std::vector<vector<int>>& edgeL) {
visit[now]=1;
for(int elem: edgeL[now])
if (elem!=parent && !visit[elem])
dfs(now,elem,visit,edgeL);
return;
}
bool isTree(std::vector<int>& r, std::vector<int>& u, std::vector<int>& v) {
int n = r.size()+1;
std::vector<vector<int>> edgeL(n);
for(int i=0; i<n-1; i++) {
int s = u[r[i]], e = v[r[i]];
edgeL[s].push_back(e);
edgeL[e].push_back(s);
}
vector<int> visit(n,0);
dfs(-1,u[r[0]],visit,edgeL);
for(int i=0; i<n; i++)
if(!visit[i]) return false;
return true;
}
bool selectEdges(int cnt, int start, std::vector<int>& r, std::vector<int>& u, std::vector<int>& v){
int n = r.size()+1, m = u.size();
if (cnt == n-1){
if(isTree(r,u,v) && count_common_roads(r)== n-1) return true;
else return false;
}
for(int i=start; i<m; i++){
r[cnt] = i;
if (selectEdges(cnt+1,i+1,r,u,v)) return true;
}
return false;
}
std::vector<int> subtask1(int n, std::vector<int>& u, std::vector<int>& v) {
std::vector<int> r(n-1), zero(n-1,-1);
if (selectEdges(0,0,r,u,v)) return r;
else return zero;
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
return subtask3(n,u,v);
// if (isSubtask1(n))
return subtask1(n,u,v);
}
Compilation message
simurgh.cpp: In function 'std::vector<int> subtask3(int, std::vector<int>&, std::vector<int>&)':
simurgh.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int j=0; j<r2Idx.size(); j++) {
| ~^~~~~~~~~~~~~
simurgh.cpp:88:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int j=1; j<result.size(); j++)
| ~^~~~~~~~~~~~~~
simurgh.cpp: In function 'void dfs(int, int, std::vector<int>&, std::vector<std::vector<int> >&)':
simurgh.cpp:105:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
105 | for(int elem: edgeL[now])
| ^~~
simurgh.cpp:108:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
108 | return;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
1 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
1 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
2 ms |
348 KB |
correct |
16 |
Correct |
2 ms |
348 KB |
correct |
17 |
Correct |
2 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
600 KB |
correct |
19 |
Correct |
2 ms |
528 KB |
correct |
20 |
Correct |
2 ms |
436 KB |
correct |
21 |
Correct |
2 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
440 KB |
correct |
23 |
Correct |
2 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
2 ms |
348 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
1 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
2 ms |
348 KB |
correct |
16 |
Correct |
2 ms |
348 KB |
correct |
17 |
Correct |
2 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
600 KB |
correct |
19 |
Correct |
2 ms |
528 KB |
correct |
20 |
Correct |
2 ms |
436 KB |
correct |
21 |
Correct |
2 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
440 KB |
correct |
23 |
Correct |
2 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
2 ms |
348 KB |
correct |
34 |
Incorrect |
77 ms |
2488 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Incorrect |
71 ms |
6384 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
1 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
2 ms |
348 KB |
correct |
16 |
Correct |
2 ms |
348 KB |
correct |
17 |
Correct |
2 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
600 KB |
correct |
19 |
Correct |
2 ms |
528 KB |
correct |
20 |
Correct |
2 ms |
436 KB |
correct |
21 |
Correct |
2 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
440 KB |
correct |
23 |
Correct |
2 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
2 ms |
348 KB |
correct |
34 |
Incorrect |
77 ms |
2488 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |