QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425412#4229. GCD HarmonysitablechairWA 1ms3716kbC++141.1kb2024-05-30 10:35:072024-05-30 10:35:08

Judging History

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

  • [2024-05-30 10:35:08]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3716kb
  • [2024-05-30 10:35:07]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb emplace_back
#define all(x) x.begin(),x.end()
#define sz(a) (signed)a.size()

#ifdef NCGM
#include"debug.h"
#else 
#define debug(...) "fr";
#endif
    
using namespace std;

const int N=5002,MAX=10000;

vector<int> adj[N];
int a[N],b[N];
bool vs[N];
ll res=0;
void dfs(int u,int cur) {
	if (vs[u]) return;
	vs[u]=1;
	if (b[u]%cur) b[u]=cur,res+=cur;
	for(auto v: adj[u]) {
		if (!vs[v]) dfs(v,cur);
	}
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    For(i,1,n) cin >> a[i];
    For(i,1,n-1) {
    	int a,b;
    	cin >> a >> b;
    	adj[a].pb(b);
    	adj[b].pb(a);
    }
    ll ans=LLONG_MAX;
    For(i,2,MAX) {
    	res=0;
    	For(j,1,n) b[j]=a[j];
    	For(j,1,n) vs[j]=0;
    	dfs(1,i);
    	ans=min(ans,res);
    }
    cout << ans;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
5
6
3
4
9
12
1 2
1 3
1 4
1 6
3 5

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3716kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3712kb

input:

13
2
5
5
5
5
5
5
3
3
3
3
3
3
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13

output:

21

result:

wrong answer 1st lines differ - expected: '15', found: '21'