QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#422958 | #7177. Many Many Cycles | HHH666 | WA | 22ms | 4088kb | C++14 | 3.4kb | 2024-05-27 20:30:14 | 2024-05-27 20:30:15 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define i1 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define all(v) v.begin(), v.end()
using namespace std;
template<typename types>
void read(types &x) {
x = 0; char c = getchar(); int f = 1;
while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f; return;
}
void read() {}
template<typename types, typename... Args>
void read(types &x, Args&... args) {
read(x), read(args...);
return;
}
template<typename types>
void write(types x) {
if (x < 0) putchar('-'), x = -x;
types k = x / 10;
if (k) write(k);
putchar(x - k * 10 + '0');
return;
}
void print() { puts(""); }
template<typename types, typename... Args>
void print(types x, Args... args) {
write(x), putchar(' '), print(args...);
return;
}
const int MAXN = 5e3 + 10;
const int MAXM = 1e4 + 10;
const int LGN = 20;
int n, m;
struct Edge {
int x, y, w;
} e[MAXM];
int head[MAXN], to[MAXM], w[MAXM], nxt[MAXM], idx = 1;
ll depth[MAXN];
int dfn[MAXN], pa[MAXN], rk[MAXN], mn[MAXN][LGN], lg[MAXN], timecnt;
int cmp(int x, int y) { return dfn[x] < dfn[y] ? x : y; }
void addedge(int x, int y, int z) {
nxt[++idx] = head[x];
to[idx] = y;
w[idx] = z;
head[x] = idx;
return;
}
int query(int l, int r) {
int k = lg[r - l + 1];
return cmp(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int lca(int x, int y) {
if (x == y) return x;
if (dfn[x] > dfn[y]) swap(x, y);
return pa[query(dfn[x] + 1, dfn[y])];
}
void dfs(int x) {
dfn[x] = ++timecnt, rk[dfn[x]] = x;
mn[rk[x]][0] = x;
for (int i = head[x]; i; i = nxt[i]) {
int y = to[i];
if (!dfn[y]) depth[y] = depth[x] + w[i], pa[y] = x, dfs(y);
}
return;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int main() {
ios:: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
read(n, m);
for (int i = 1; i <= m; ++i) {
int x, y, z; read(x, y, z);
addedge(x, y, z), addedge(y, x, z);
e[i].x = x, e[i].y = y, e[i].w = z;
}
dfs(1);
for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int k = 1; k <= lg[n]; ++k)
for (int i = 1; i + (1 << k) - 1 <= n; ++i)
mn[i][k] = cmp(mn[i][k - 1], mn[i + (1 << k - 1)][k - 1]);
ll g = -1;
for (int i = 1; i <= m; ++i) {
if (depth[e[i].x] > depth[e[i].y]) swap(e[i].x, e[i].y);
int x = e[i].x, y = e[i].y, w = e[i].w;
if (pa[y] == x) continue;
ll t = depth[y] - depth[x] + w;
if (~g) g = gcd(g, t);
else g = t;
}
if (g == -1) { print(0); return 0; }
for (int i = 1; i <= m; ++i) {
int x1 = e[i].x, y1 = e[i].y;
if (pa[y1] == x1) continue;
for (int j = i + 1; j <= m; ++j) {
int x2 = e[j].x, y2 = e[j].y;
if (pa[y2] == x2) continue;
int l = lca(y1, y2);
if (dfn[x1] < dfn[x2]) swap(x1, x2);
int x = x1;
if (lca(x, l) != x) continue;
g = gcd(g, 2 * (depth[l] - depth[x]));
}
}
print(g);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3696kb
input:
4 4 1 2 1 2 3 1 3 4 1 4 1 1
output:
4
result:
ok answer is '4'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
4 5 1 2 1 1 3 2 1 4 1 2 3 1 3 4 1
output:
4
result:
ok answer is '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
20 50 1 2 8 1 3 1 3 4 5 3 5 9 3 6 5 6 7 6 7 8 8 2 9 2 8 10 3 8 11 7 8 12 5 3 13 4 7 14 3 6 15 7 9 16 6 8 17 7 16 18 9 16 19 3 18 20 10 11 3 2 17 1 1 16 2 2 15 1 1 10 3 2 9 1 2 19 2 1 6 1 2 7 3 1 17 3 2 15 3 2 8 6 2 5 1 2 8 1 2 12 1 1 12 7 1 4 1 2 18 2 1 11 7 1 14 1 1 18 1 1 18 9 1 10 6 1 14 3 2 20 2...
output:
2
result:
ok answer is '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
20 50 1 2 18468 1 3 26501 3 4 15725 3 5 29359 3 6 24465 6 7 28146 7 8 16828 2 9 492 8 10 11943 8 11 5437 8 12 14605 3 13 154 7 14 12383 6 15 18717 9 16 19896 8 17 21727 16 18 11539 16 19 19913 18 20 26300 11 3 2 17 1 1 16 2 2 15 1 1 10 3 2 9 1 2 19 2 1 6 1 2 7 3 1 17 3 2 15 3 2 8 6 2 5 1 2 8 1 2 12 ...
output:
1
result:
ok answer is '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
100 150 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 11...
output:
3
result:
ok answer is '3'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
100 130 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 11...
output:
7
result:
ok answer is '7'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
100 200 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 11...
output:
4
result:
ok answer is '4'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
100 190 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 11...
output:
2
result:
ok answer is '2'
Test #9:
score: 0
Accepted
time: 2ms
memory: 3740kb
input:
1000 1500 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
3
result:
ok answer is '3'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3864kb
input:
1000 1500 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
1
result:
ok answer is '1'
Test #11:
score: 0
Accepted
time: 3ms
memory: 3920kb
input:
1000 1600 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
1
result:
ok answer is '1'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
100 190 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 11...
output:
4
result:
ok answer is '4'
Test #13:
score: 0
Accepted
time: 22ms
memory: 3912kb
input:
1000 3000 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
2
result:
ok answer is '2'
Test #14:
score: 0
Accepted
time: 2ms
memory: 3792kb
input:
1000 1400 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
1
result:
ok answer is '1'
Test #15:
score: 0
Accepted
time: 2ms
memory: 3924kb
input:
1000 1500 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
2
result:
ok answer is '2'
Test #16:
score: 0
Accepted
time: 3ms
memory: 3796kb
input:
1000 1500 1 2 184676335 1 3 191705725 1 4 293606963 1 5 57078146 2 6 168279962 6 7 29961943 5 8 54392392 5 9 39020154 5 10 123837422 7 11 197199896 3 12 217274772 7 13 18709913 6 14 263007036 11 15 287053812 3 16 303347674 9 17 151417712 17 18 68705548 15 19 326652758 12 20 128598724 2 21 275290779 ...
output:
8
result:
ok answer is '8'
Test #17:
score: 0
Accepted
time: 3ms
memory: 4000kb
input:
2000 2500 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
12
result:
ok answer is '12'
Test #18:
score: 0
Accepted
time: 3ms
memory: 4080kb
input:
2000 2500 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
2
result:
ok answer is '2'
Test #19:
score: 0
Accepted
time: 3ms
memory: 3928kb
input:
2000 2500 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
13
result:
ok answer is '13'
Test #20:
score: 0
Accepted
time: 3ms
memory: 4060kb
input:
2000 2500 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
1
result:
ok answer is '1'
Test #21:
score: 0
Accepted
time: 8ms
memory: 4088kb
input:
2000 3000 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
3
result:
ok answer is '3'
Test #22:
score: -100
Wrong Answer
time: 5ms
memory: 4084kb
input:
2000 3000 1 2 22794613 1 3 23670271 1 4 36254735 1 5 7068116 2 6 20774480 6 7 3707773 5 8 6740416 5 9 4815222 5 10 15296810 7 11 24351908 3 12 26824656 7 13 2326259 6 14 32470002 11 15 35443314 3 16 37448596 9 17 18691706 17 18 8500660 15 19 40337666 12 20 15876730 2 21 33971565 11 22 3767381 17 23 ...
output:
3
result:
wrong answer expected '1', found '3'