QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#210894#7177. Many Many Cyclesucup-team191#WA 1ms4068kbC++142.4kb2023-10-11 21:11:482023-10-11 21:11:48

Judging History

你现在查看的是最新测评结果

  • [2023-10-11 21:11:48]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4068kb
  • [2023-10-11 21:11:48]
  • 提交

answer

#include <bits/stdc++.h>
#define X first
#define Y second
#define x first
#define y second
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)

const int N=10010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;

int n, m;
vector < pii > v[N];
ll dep[N], d;
int par[N][15], deep[N];
vector < pii > luk;
vector < int > smece;

void dfs(int x, int lst) {
	par[x][0] = lst;
	for(int j = 1;j < 15;j++)
		par[x][j] = par[par[x][j - 1]][j - 1];
	for(pii tmp : v[x]) {
		if(tmp.Y == lst) continue;
		if(dep[tmp.Y] != -1 && dep[tmp.Y] < dep[x]) {
			luk.pb({x, tmp.Y});
			smece.pb(tmp.X);
		} else if(dep[tmp.Y] == -1) {
			dep[tmp.Y] = dep[x] + tmp.X;
			deep[tmp.Y] = deep[x] + 1;
			dfs(tmp.Y, x);
		}
	}
}

int digni(int x, int k){
	for(int j = 0;j < 15;j++)
		if(k & (1 << j)) x = par[x][j];
	return x;
}

ll gcdl(ll A, ll B) {
    if(!B) return A;
    return gcdl(B, A % B);
}

int main()
{

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	memset(dep, -1, sizeof(dep));
	cin >> n >> m;
	for(int i = 0;i < m;i++) {
		int a, b, c; cin >> a >> b >> c;
		v[a].pb({c, b});
		v[b].pb({c, a});
	}
	ll d = 0;
	for(int x = 1;x <= n;x++) {
		if(dep[x] != -1) continue;
		dep[x] = 0; dfs(x, x); deep[x] = 0;
		for(int i = 0;i < (int)luk.size();i++) {
		//	cout << " luk " << luk[i].x << " " << luk[i].y << std::endl;
			d = gcdl(d, dep[luk[i].x] - dep[luk[i].y] + smece[i]);
			for(int j = 0;j < (int)luk.size();j++) {
				if(i == j) continue;
				if(dep[luk[i].y] > dep[luk[j].y]) continue;
				if(dep[luk[i].x] > dep[luk[j].x]) continue;
				if(dep[luk[j].y] > dep[luk[i].x]) continue;
				if(digni(luk[j].x, deep[luk[j].x] - deep[luk[i].x]) != luk[i].x) continue;
				d = gcdl(d, 2LL * (dep[luk[i].x] - dep[luk[j].y]));
			}
		}
		for(int i = 0;i < (int)luk.size();i++) {
		//	cout << " luk " << luk[i].x << " " << luk[i].y << std::endl;
			d = gcdl(d, dep[luk[i].x] - dep[luk[i].y] + smece[i]);
			for(int j = 0;j < (int)luk.size();j++) {
				if(i == j) continue;
				if(dep[luk[i].y] > dep[luk[j].y]) continue;
				if(dep[luk[i].x] < dep[luk[j].x]) continue;
				if(digni(luk[i].x, deep[luk[i].x] - deep[luk[j].x]) != luk[j].x) continue;
				d = gcdl(d, 2LL * (dep[luk[i].x] - dep[luk[j].y]));
			}
		}
		luk.clear(); smece.clear();
	}
	cout << d << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4068kb

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: -100
Wrong Answer
time: 1ms
memory: 3996kb

input:

4 5
1 2 1
1 3 2
1 4 1
2 3 1
3 4 1

output:

2

result:

wrong answer expected '4', found '2'