#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#define N 500005
#define NN 1005000
#define PB push_back
#define M ll(1e9 + 7)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pri(x) cout << x << endl
#define endl '\n'
#define _ << " " <<
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> oredered_set;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef short int si;
int n, ans = 0;
vector <int> g[N], vr;
string s;
void dfs(int v, int p)
{
vr.PB(v);
for (auto it : g[v])
{
if (it == p) continue;
dfs(it, v);
}
}
void sub_2()
{
int ans = 0, v;
for (int i = 1; i <= n; i++)
if (sz(g[i]) == 1)
v = i;
dfs(v, -1);
int l = -1, r = -1e9;
for (int i = 0; i < n; i++)
if (s[vr[i] - 1] == '0')
{
r = i;
if (l == -1)
l = i;
}
for (int i = l; i < r; i++)
{
int v = vr[i];
ans++;
if (s[v] == '0')
{
s[v] = '1';
continue;
}
ans += 2;
}
pri(ans);
exit(0);
}
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("1.in", "r", stdin);
cin >> n;
cin >> s;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].PB(y);
g[y].PB(x);
}
bool ft = 1;
for (int i = 1; i <= n; i++)
if (sz(g[i]) > 2)
{
ft = 0;
break;
}
if (ft)
sub_2();
}
Compilation message
svjetlo.cpp: In function 'void sub_2()':
svjetlo.cpp:58:8: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
58 | dfs(v, -1);
| ~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
209 ms |
43508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
211 ms |
30080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |