#include "simurgh.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 500 + 5;
vector<int> adj[maxn];
int par[maxn];
ii e[maxn * maxn];
int dep[maxn];
int state[maxn * maxn];
void dfs(int u , int par){
for(auto & c : adj[u]){
int v = e[c].first + e[c].second - u;
if(v == par)continue;
::par[v] = c;
dep[v] = dep[u] + 1;
dfs(v , u);
}
}
int lab[maxn];
int FindLab(int u){
return lab[u] < 0 ? u : lab[u] = FindLab(lab[u]);
}
bool Merge(int u , int v , int c){
int s = FindLab(u) , d = FindLab(v);
if(s == d)return 0;
adj[u].pb(c);
adj[v].pb(c);
if(lab[s] > lab[d])swap(s,d);
lab[s] += lab[d];lab[d] = s;
return 1;
}
vector<int> pick(int n , int m){
vector<int> cur;
for(int i = 0 ; i < n ; ++i)lab[i] = -1 , adj[i].clear();
for(int i = 0 ; i < m ; ++i){
if(state[i] == 1){
if(Merge(e[i].first,e[i].second , i))cur.pb(i);
}
}
for(int i = 0 ; i < m ; ++i){
if(state[i] == -1){
if(Merge(e[i].first,e[i].second , i))cur.pb(i);
}
}
return cur;
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
memset(state,-1,sizeof state);
int m = u.size();
for(int i = 0 ; i < m ; ++i){
e[i] = mp(u[i] , v[i]);
}
auto cur = pick(n , m);
int cur_common = count_common_roads(cur);
dfs(0 , -1);
for(int i = 0 ; i < m ; ++i){
int u , v;tie(u,v) = e[i];
vector<int> val;
while(u != v){
if(dep[u] < dep[v])swap(u,v);
if(state[par[u]] == -1)val.pb(par[u]);
u = e[par[u]].first + e[par[u]].second - u;
}
if(val.size() == 0)continue;
if(val.size() == 1 && val[0] == i)continue;
bool ok = 1;
for(auto & c : val){
cur.erase(find(cur.begin(),cur.end(),c));
cur.pb(i);
int tmp = count_common_roads(cur);
if(tmp > cur_common){
state[i] = 1;
cur_common = tmp;
ok = 0;
adj[e[c].first].erase(find(adj[e[c].first].begin() , adj[e[c].first].end(),c));
adj[e[c].second].erase(find(adj[e[c].second].begin() , adj[e[c].second].end(),c));
adj[e[i].first].pb(i);adj[e[i].second].pb(i);
dfs(0,-1);
break;
}else{
cur.pop_back();
cur.pb(c);
}
if(tmp < cur_common){
state[i] = 0;
state[c] = 1;
ok = 0;
break;
}
}
if(ok){
state[i] = 0;
for(auto & c : val)state[c] = 0;
cur = pick(n , m);
dfs(0,-1);
cur_common = count_common_roads(cur);
}
}
return cur;
}
#ifdef LOCAL
#include "simurgh.h"
#include <cstdio>
#include <cassert>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
static int MAXQ = 30000;
static int n, m, q = 0;
static vector<int> u, v;
static vector<bool> goal;
static void wrong_answer() {
printf("NO\n");
exit(0);
}
static bool is_valid(const vector<int>& r) {
if(int(r.size()) != n - 1)
return false;
for(int i = 0; i < n - 1; i++)
if (r[i] < 0 || r[i] >= m)
return false;
return true;
}
static int _count_common_roads_internal(const vector<int>& r) {
if(!is_valid(r))
wrong_answer();
int common = 0;
for(int i = 0; i < n - 1; i++) {
bool is_common = goal[r[i]];
if (is_common)
common++;
}
return common;
}
int count_common_roads(const vector<int>& r) {
q++;
if(q > MAXQ)
wrong_answer();
return _count_common_roads_internal(r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
assert(2 == scanf("%d %d", &n, &m));
u.resize(m);
v.resize(m);
for(int i = 0; i < m; i++)
assert(2 == scanf("%d %d", &u[i], &v[i]));
goal.resize(m, false);
for(int i = 0; i < n - 1; i++) {
int id;
assert(1 == scanf("%d", &id));
goal[id] = true;
}
vector<int> res = find_roads(n, u, v);
if(_count_common_roads_internal(res) != n - 1)
wrong_answer();
printf("YES\n");
return 0;
}
#endif // LOCAL
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
correct |
2 |
Correct |
1 ms |
1280 KB |
correct |
3 |
Correct |
1 ms |
1280 KB |
correct |
4 |
Correct |
1 ms |
1280 KB |
correct |
5 |
Correct |
1 ms |
1280 KB |
correct |
6 |
Correct |
1 ms |
1280 KB |
correct |
7 |
Correct |
1 ms |
1280 KB |
correct |
8 |
Correct |
1 ms |
1280 KB |
correct |
9 |
Correct |
1 ms |
1280 KB |
correct |
10 |
Correct |
1 ms |
1280 KB |
correct |
11 |
Correct |
1 ms |
1280 KB |
correct |
12 |
Correct |
1 ms |
1280 KB |
correct |
13 |
Correct |
1 ms |
1280 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
correct |
2 |
Correct |
1 ms |
1280 KB |
correct |
3 |
Correct |
1 ms |
1280 KB |
correct |
4 |
Correct |
1 ms |
1280 KB |
correct |
5 |
Correct |
1 ms |
1280 KB |
correct |
6 |
Correct |
1 ms |
1280 KB |
correct |
7 |
Correct |
1 ms |
1280 KB |
correct |
8 |
Correct |
1 ms |
1280 KB |
correct |
9 |
Correct |
1 ms |
1280 KB |
correct |
10 |
Correct |
1 ms |
1280 KB |
correct |
11 |
Correct |
1 ms |
1280 KB |
correct |
12 |
Correct |
1 ms |
1280 KB |
correct |
13 |
Correct |
1 ms |
1280 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
6 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
2 ms |
1280 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
5 ms |
1408 KB |
correct |
21 |
Correct |
5 ms |
1408 KB |
correct |
22 |
Correct |
1 ms |
1408 KB |
correct |
23 |
Correct |
1 ms |
1408 KB |
correct |
24 |
Correct |
1 ms |
1408 KB |
correct |
25 |
Correct |
1 ms |
1408 KB |
correct |
26 |
Correct |
1 ms |
1408 KB |
correct |
27 |
Correct |
1 ms |
1408 KB |
correct |
28 |
Correct |
2 ms |
1408 KB |
correct |
29 |
Correct |
1 ms |
1384 KB |
correct |
30 |
Correct |
2 ms |
1408 KB |
correct |
31 |
Correct |
2 ms |
1536 KB |
correct |
32 |
Correct |
1 ms |
1408 KB |
correct |
33 |
Correct |
1 ms |
1408 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
correct |
2 |
Correct |
1 ms |
1280 KB |
correct |
3 |
Correct |
1 ms |
1280 KB |
correct |
4 |
Correct |
1 ms |
1280 KB |
correct |
5 |
Correct |
1 ms |
1280 KB |
correct |
6 |
Correct |
1 ms |
1280 KB |
correct |
7 |
Correct |
1 ms |
1280 KB |
correct |
8 |
Correct |
1 ms |
1280 KB |
correct |
9 |
Correct |
1 ms |
1280 KB |
correct |
10 |
Correct |
1 ms |
1280 KB |
correct |
11 |
Correct |
1 ms |
1280 KB |
correct |
12 |
Correct |
1 ms |
1280 KB |
correct |
13 |
Correct |
1 ms |
1280 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
6 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
2 ms |
1280 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
5 ms |
1408 KB |
correct |
21 |
Correct |
5 ms |
1408 KB |
correct |
22 |
Correct |
1 ms |
1408 KB |
correct |
23 |
Correct |
1 ms |
1408 KB |
correct |
24 |
Correct |
1 ms |
1408 KB |
correct |
25 |
Correct |
1 ms |
1408 KB |
correct |
26 |
Correct |
1 ms |
1408 KB |
correct |
27 |
Correct |
1 ms |
1408 KB |
correct |
28 |
Correct |
2 ms |
1408 KB |
correct |
29 |
Correct |
1 ms |
1384 KB |
correct |
30 |
Correct |
2 ms |
1408 KB |
correct |
31 |
Correct |
2 ms |
1536 KB |
correct |
32 |
Correct |
1 ms |
1408 KB |
correct |
33 |
Correct |
1 ms |
1408 KB |
correct |
34 |
Correct |
1526 ms |
2308 KB |
correct |
35 |
Correct |
1379 ms |
2296 KB |
correct |
36 |
Correct |
730 ms |
2108 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
1379 ms |
2296 KB |
correct |
39 |
Correct |
1288 ms |
2168 KB |
correct |
40 |
Correct |
792 ms |
2040 KB |
correct |
41 |
Correct |
1840 ms |
2284 KB |
correct |
42 |
Correct |
1775 ms |
2296 KB |
correct |
43 |
Correct |
8 ms |
1792 KB |
correct |
44 |
Correct |
7 ms |
1792 KB |
correct |
45 |
Correct |
9 ms |
1792 KB |
correct |
46 |
Correct |
6 ms |
1664 KB |
correct |
47 |
Correct |
4 ms |
1536 KB |
correct |
48 |
Correct |
2 ms |
1408 KB |
correct |
49 |
Correct |
3 ms |
1536 KB |
correct |
50 |
Correct |
4 ms |
1536 KB |
correct |
51 |
Correct |
8 ms |
1792 KB |
correct |
52 |
Correct |
7 ms |
1664 KB |
correct |
53 |
Correct |
6 ms |
1664 KB |
correct |
54 |
Correct |
8 ms |
1792 KB |
correct |
55 |
Correct |
8 ms |
1792 KB |
correct |
56 |
Correct |
8 ms |
1792 KB |
correct |
57 |
Correct |
8 ms |
1844 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1312 KB |
correct |
2 |
Correct |
1 ms |
1280 KB |
correct |
3 |
Incorrect |
1111 ms |
4020 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
correct |
2 |
Correct |
1 ms |
1280 KB |
correct |
3 |
Correct |
1 ms |
1280 KB |
correct |
4 |
Correct |
1 ms |
1280 KB |
correct |
5 |
Correct |
1 ms |
1280 KB |
correct |
6 |
Correct |
1 ms |
1280 KB |
correct |
7 |
Correct |
1 ms |
1280 KB |
correct |
8 |
Correct |
1 ms |
1280 KB |
correct |
9 |
Correct |
1 ms |
1280 KB |
correct |
10 |
Correct |
1 ms |
1280 KB |
correct |
11 |
Correct |
1 ms |
1280 KB |
correct |
12 |
Correct |
1 ms |
1280 KB |
correct |
13 |
Correct |
1 ms |
1280 KB |
correct |
14 |
Correct |
7 ms |
1408 KB |
correct |
15 |
Correct |
6 ms |
1408 KB |
correct |
16 |
Correct |
6 ms |
1408 KB |
correct |
17 |
Correct |
6 ms |
1408 KB |
correct |
18 |
Correct |
2 ms |
1280 KB |
correct |
19 |
Correct |
6 ms |
1408 KB |
correct |
20 |
Correct |
5 ms |
1408 KB |
correct |
21 |
Correct |
5 ms |
1408 KB |
correct |
22 |
Correct |
1 ms |
1408 KB |
correct |
23 |
Correct |
1 ms |
1408 KB |
correct |
24 |
Correct |
1 ms |
1408 KB |
correct |
25 |
Correct |
1 ms |
1408 KB |
correct |
26 |
Correct |
1 ms |
1408 KB |
correct |
27 |
Correct |
1 ms |
1408 KB |
correct |
28 |
Correct |
2 ms |
1408 KB |
correct |
29 |
Correct |
1 ms |
1384 KB |
correct |
30 |
Correct |
2 ms |
1408 KB |
correct |
31 |
Correct |
2 ms |
1536 KB |
correct |
32 |
Correct |
1 ms |
1408 KB |
correct |
33 |
Correct |
1 ms |
1408 KB |
correct |
34 |
Correct |
1526 ms |
2308 KB |
correct |
35 |
Correct |
1379 ms |
2296 KB |
correct |
36 |
Correct |
730 ms |
2108 KB |
correct |
37 |
Correct |
14 ms |
1408 KB |
correct |
38 |
Correct |
1379 ms |
2296 KB |
correct |
39 |
Correct |
1288 ms |
2168 KB |
correct |
40 |
Correct |
792 ms |
2040 KB |
correct |
41 |
Correct |
1840 ms |
2284 KB |
correct |
42 |
Correct |
1775 ms |
2296 KB |
correct |
43 |
Correct |
8 ms |
1792 KB |
correct |
44 |
Correct |
7 ms |
1792 KB |
correct |
45 |
Correct |
9 ms |
1792 KB |
correct |
46 |
Correct |
6 ms |
1664 KB |
correct |
47 |
Correct |
4 ms |
1536 KB |
correct |
48 |
Correct |
2 ms |
1408 KB |
correct |
49 |
Correct |
3 ms |
1536 KB |
correct |
50 |
Correct |
4 ms |
1536 KB |
correct |
51 |
Correct |
8 ms |
1792 KB |
correct |
52 |
Correct |
7 ms |
1664 KB |
correct |
53 |
Correct |
6 ms |
1664 KB |
correct |
54 |
Correct |
8 ms |
1792 KB |
correct |
55 |
Correct |
8 ms |
1792 KB |
correct |
56 |
Correct |
8 ms |
1792 KB |
correct |
57 |
Correct |
8 ms |
1844 KB |
correct |
58 |
Correct |
1 ms |
1312 KB |
correct |
59 |
Correct |
1 ms |
1280 KB |
correct |
60 |
Incorrect |
1111 ms |
4020 KB |
WA in grader: NO |
61 |
Halted |
0 ms |
0 KB |
- |