Submission #44693925
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define x first
#define y second
#define mp(x, y) make_pair(x, y)
#define fro(i, x, y) for(int i = (x);i <= (y);i++)
#define pre(i, x, y) for(int i = (x);i >= (y);i--)
typedef long long ll;
typedef __int128 lll;
// typedef unsigned int uint;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int N = 1000010;
const int mod = 998244353;
int n, m, cnt, a[N], fa[N], head[N]; ll num, ans;
struct edge { int to, nxt; } e[N];
inline void add(int x, int y)
{
e[++cnt] = {y, head[x]}, head[x] = cnt;
e[++cnt] = {x, head[y]}, head[y] = cnt;
}
inline pair<ll, ll> dfs(int now, int fa)
{
ll w = 0, h = 1; if(a[now]) h = 0, w += a[now] - 1;
for(int i = head[now];i;i = e[i].nxt)
{
if(e[i].to == fa) continue;
auto j = dfs(e[i].to, now);
num += j.x + j.y, w += j.x, h += j.y;
}
ll x = min(w, h); w -= x, h -= x;
if(now == 1 && (w || h)) num = -1;
return mp(h, w);
}
inline int gf(int x)
{
return (fa[x] == x ? x : fa[x] = gf(fa[x]));
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m; int vx = 0, vy = 0;
iota(fa + 1, fa + n + 1, 1), ans = 1e17;
fro(i, 1, m)
{
int x, y; cin >> x >> y;
if(gf(x) == gf(y))
vx = x, vy = y;
else add(x, y), fa[gf(x)] = gf(y);
}
if(!vx && !vy)
num = 0, dfs(1, 0), ans = num;
else
{
auto x = dfs(1, 0);
ans = min(ans, num);
int flag = num;
a[vx] = a[vy] = 1, num = 1;
auto y = dfs(1, 0);
if(num != -1) ans = min(ans, num);
if(num != -1 && flag != -1 && num != flag)
{
int op = (flag > num ? 1 : -1), now = 0;
for(int i = 28;i >= 0;i--)
{
now = now + (1 << i);
a[vx] = a[vy] = op * now, num = now, dfs(1, 0); int f1 = num;
a[vx] = a[vy] = op * (now + 1), num = (now + 1), dfs(1, 0); int f2 = num;
if(f1 < f2) now = now - (1 << i);
}
a[vx] = a[vy] = op * now, num = now, dfs(1, 0);
ans = min(ans, num);
a[vx] = a[vy] = op * (now + 1), num = (now + 1), dfs(1, 0);
ans = min(ans, num);
}
else if(x.x || x.y)
{
if(x.x && !y.y && x.x != y.x)
{
int g = x.x - y.x;
if(x.x % g == 0)
{
a[vx] = a[vy] = x.x / g;
num = abs(x.x / g), dfs(1, 0), ans = num;
}
}
if(x.y && !y.x && x.y != y.y)
{
int g = x.y - y.y;
if(x.y % g == 0)
{
a[vx] = a[vy] = x.y / g;
num = abs(x.y / g), dfs(1, 0), ans = num;
}
}
}
}
cout << ans << "\n";
return 0;
}
Submission Info
| Submission Time |
|
| Task |
F - Namori |
| User |
win114514 |
| Language |
C++ (GCC 9.2.1) |
| Score |
2200 |
| Code Size |
3045 Byte |
| Status |
AC |
| Exec Time |
375 ms |
| Memory |
14240 KiB |
Judge Result
| Set Name |
Sample |
Subtask1 |
All |
| Score / Max Score |
0 / 0 |
1500 / 1500 |
700 / 700 |
| Status |
|
|
|
| Set Name |
Test Cases |
| Sample |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
| Subtask1 |
0_00.txt, 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt |
| All |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 0_00.txt |
AC |
7 ms |
3524 KiB |
| 0_01.txt |
AC |
2 ms |
3520 KiB |
| 0_02.txt |
AC |
2 ms |
3516 KiB |
| 0_03.txt |
AC |
2 ms |
3528 KiB |
| 1_00.txt |
AC |
2 ms |
3632 KiB |
| 1_01.txt |
AC |
38 ms |
14240 KiB |
| 1_02.txt |
AC |
33 ms |
9984 KiB |
| 1_03.txt |
AC |
28 ms |
8276 KiB |
| 1_04.txt |
AC |
35 ms |
10948 KiB |
| 1_05.txt |
AC |
36 ms |
10704 KiB |
| 1_06.txt |
AC |
34 ms |
10772 KiB |
| 1_07.txt |
AC |
33 ms |
10028 KiB |
| 1_08.txt |
AC |
34 ms |
11348 KiB |
| 1_09.txt |
AC |
30 ms |
8236 KiB |
| 1_10.txt |
AC |
32 ms |
8344 KiB |
| 1_11.txt |
AC |
36 ms |
8236 KiB |
| 1_12.txt |
AC |
32 ms |
8232 KiB |
| 1_13.txt |
AC |
33 ms |
8732 KiB |
| 1_14.txt |
AC |
29 ms |
8288 KiB |
| 1_15.txt |
AC |
36 ms |
8328 KiB |
| 1_16.txt |
AC |
33 ms |
8240 KiB |
| 1_17.txt |
AC |
31 ms |
8228 KiB |
| 2_00.txt |
AC |
2 ms |
3532 KiB |
| 2_01.txt |
AC |
49 ms |
14168 KiB |
| 2_02.txt |
AC |
264 ms |
10020 KiB |
| 2_03.txt |
AC |
89 ms |
8700 KiB |
| 2_04.txt |
AC |
340 ms |
10560 KiB |
| 2_05.txt |
AC |
362 ms |
10904 KiB |
| 2_06.txt |
AC |
375 ms |
11056 KiB |
| 2_07.txt |
AC |
347 ms |
10968 KiB |
| 2_08.txt |
AC |
367 ms |
11212 KiB |
| 2_09.txt |
AC |
190 ms |
8376 KiB |
| 2_10.txt |
AC |
223 ms |
8244 KiB |
| 2_11.txt |
AC |
34 ms |
8248 KiB |
| 2_12.txt |
AC |
274 ms |
8220 KiB |
| 2_13.txt |
AC |
40 ms |
11004 KiB |
| 2_14.txt |
AC |
39 ms |
8348 KiB |
| 2_15.txt |
AC |
275 ms |
8288 KiB |
| 2_16.txt |
AC |
37 ms |
8296 KiB |
| 2_17.txt |
AC |
273 ms |
8240 KiB |
| 2_18.txt |
AC |
2 ms |
3464 KiB |
| 2_19.txt |
AC |
44 ms |
9888 KiB |
| 2_20.txt |
AC |
26 ms |
8648 KiB |
| 2_21.txt |
AC |
44 ms |
12028 KiB |
| 2_22.txt |
AC |
44 ms |
10944 KiB |
| 2_23.txt |
AC |
44 ms |
9800 KiB |
| 2_24.txt |
AC |
46 ms |
9952 KiB |
| 2_25.txt |
AC |
43 ms |
10372 KiB |
| 2_26.txt |
AC |
44 ms |
10396 KiB |
| 2_27.txt |
AC |
38 ms |
8244 KiB |
| 2_28.txt |
AC |
41 ms |
8336 KiB |
| 2_29.txt |
AC |
38 ms |
8336 KiB |
| 2_30.txt |
AC |
40 ms |
8228 KiB |
| 2_31.txt |
AC |
40 ms |
9436 KiB |
| 2_32.txt |
AC |
41 ms |
8508 KiB |
| 2_33.txt |
AC |
40 ms |
8320 KiB |
| 2_34.txt |
AC |
36 ms |
8308 KiB |
| 2_35.txt |
AC |
41 ms |
8236 KiB |
| 2_36.txt |
AC |
87 ms |
8280 KiB |
| 2_37.txt |
AC |
88 ms |
8980 KiB |
| 2_38.txt |
AC |
88 ms |
8484 KiB |
| 2_39.txt |
AC |
88 ms |
8164 KiB |
| 2_40.txt |
AC |
88 ms |
8492 KiB |
| 2_41.txt |
AC |
87 ms |
8276 KiB |
| 2_42.txt |
AC |
92 ms |
8588 KiB |
| 2_43.txt |
AC |
87 ms |
8416 KiB |