QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#233140 | #7177. Many Many Cycles | ikefumy | WA | 1ms | 6612kb | C++20 | 5.9kb | 2023-10-31 13:58:56 | 2023-10-31 13:58:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define int128 __int128_t
#define pii128 pair<int128,int128>
const int inf = 1 << 30;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
const db EPS = 1e-10;
const db pi = acos(-1);
template<class T> bool chmin(T& x, T y){
if(x > y) {
x = y;
return true;
} else return false;
}
template<class T> bool chmax(T& x, T y){
if(x < y) {
x = y;
return true;
} else return false;
}
// overload macro
#define CAT( A, B ) A ## B
#define SELECT( NAME, NUM ) CAT( NAME, NUM )
#define GET_COUNT( _1, _2, _3, _4, _5, _6 /* ad nauseam */, COUNT, ... ) COUNT
#define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 6, 5, 4, 3, 2, 1 )
#define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)
// rep(overload)
#define rep( ... ) VA_SELECT(rep, __VA_ARGS__)
#define rep2(i, n) for (int i = 0; i < int(n); i++)
#define rep3(i, a, b) for (int i = a; i < int(b); i++)
#define rep4(i, a, b, c) for (int i = a; i < int(b); i += c)
// repll(overload)
#define repll( ... ) VA_SELECT(repll, __VA_ARGS__)
#define repll2(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repll3(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define repll4(i, a, b, c) for (ll i = a; i < (ll)(b); i += c)
// rrep(overload)
#define rrep( ... ) VA_SELECT(rrep, __VA_ARGS__)
#define rrep2(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep3(i, a, b) for (int i = b - 1; i >= a; i--)
#define rrep4(i, a, b, c) for (int i = b - 1; i >= a; i -= c)
// rrepll(overload)
#define rrepll( ... ) VA_SELECT(rrepll, __VA_ARGS__)
#define rrepll2(i, n) for (ll i = (ll)(n) - 1; i >= 0ll; i--)
#define rrepll3(i, a, b) for (ll i = b - 1; i >= (ll)(a); i--)
#define rrepll4(i, a, b, c) for (ll i = b - 1; i >= (ll)(a); i -= c)
// for_earh
#define fore(e, v) for (auto&& e : v)
// vector
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
struct LCA{
int N;
int logn;
vector<vector<int>> G;
vector<vector<int>> par;
vector<int> depth, used;
LCA(int n) : N(n), logn(0), G(n + 1), depth(n + 1), used(n + 1) {
while((1 << logn) <= N) logn++;
par = vector<vector<int>>(logn, vector<int>(n + 1));
}
void add_edge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
}
void dfs(int v, int p, int d){
par[0][v] = p;
depth[v] = d;
used[v] = true;
for(auto& u : G[v]){
if(p == u) continue;
dfs(u, v, d + 1);
}
}
void setting(){
for (int i = 1; i <= N; i++) {
if (used[i]) continue;
dfs(i, -1, 0);
}
for(int i = 1; i < logn; i++){
for(int j = 1; j <= N; j++){
if(par[i - 1][j] < 0) par[i][j] = -1;
else par[i][j] = par[i - 1][par[i - 1][j]];
}
}
}
int get_lca(int u, int v){
if(depth[u] < depth[v]) swap(u, v);
int d = depth[u] - depth[v];
for(int i = 0; i < logn; i++){
if(d >> i & 1) u = par[i][u];
}
if(u == v) return u;
for(int i = logn - 1; i >= 0; i--){
if(par[i][u] != par[i][v]){
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
int get_len(int u, int v) {
int cp = get_lca(u, v);
return depth[u] + depth[v] - depth[cp] * 2;
}
};
int N, M, par[5010], lc[5010][5010];
ll depth[5010], g;
vector<pil> G[5010];
vector<pil> TG[5010];
vector<pil> rem[5010];
bool used[5010];
LCA lca(0);
void dfs(int v, ll d, vector<int>& vs) {
vs.push_back(v);
used[v] = true;
depth[v] = d;
for (auto& [u, w] : G[v]) {
if (used[u]) {
if (u != par[v] && u < v) {
rem[v].emplace_back(u, w);
rem[u].emplace_back(v, w);
}
continue;
}
par[u] = v;
TG[v].emplace_back(u, w);
TG[u].emplace_back(v, w);
lca.add_edge(u, v);
dfs(u, d + w, vs);
}
}
void make_ans(int v, int p) {
used[v] = true;
for (auto&& [u, w] : TG[v]) {
if (u == p) continue;
make_ans(u, v);
}
for (auto&& [u, w] : rem[v]) {
if (depth[u] > depth[v]) continue;
g = gcd(g, w + depth[v] - depth[u]);
int p = v;
while (true) {
for (auto&& [q, r] : rem[p]) {
if (depth[q] < depth[p]) continue;
if (max(u, v) == max(p, q) && min(u, v) == min(p, q)) continue;
ll len = w + r + depth[p] - depth[u];
len += depth[v] + depth[q] - 2 * depth[lc[v][q]];
g = gcd(g, len);
}
if (p == u) break;
p = par[p];
}
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
cin >> N >> M;
lca = LCA(N);
rep (i, M) {
int a, b, c;
cin >> a >> b >> c;
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
vector<vector<int>> vss;
rep (i, 1, N + 1) {
if (used[i]) continue;
vector<int> vs;
dfs(i, 0, vs);
vss.push_back(vs);
}
lca.setting();
rep (i, vss.size()) {
fore (u, vss[i]) fore (v, vss[i]) {
if (u == v) continue;
lc[u][v] = lca.get_lca(u, v);
}
}
rep (i, 1, N + 1) used[i] = false;
rep (i, 1, N + 1) {
if (used[i]) continue;
make_ans(i, 0);
}
cout << g << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6024kb
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: 4004kb
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: 1ms
memory: 6240kb
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: 4268kb
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: -100
Wrong Answer
time: 1ms
memory: 6612kb
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:
1
result:
wrong answer expected '3', found '1'