QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#547994#8760. 不等式xfs001WA 11ms54864kbC++141004b2024-09-05 14:31:592024-09-05 14:31:59

Judging History

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

  • [2024-09-05 14:31:59]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:54864kb
  • [2024-09-05 14:31:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>g[2020000];
int r[2020000],a[2020000];
void dfs(int u,int f){
	if(a[u])return;
	int x=0;
	for(auto v:g[u]){
		if(v==f)continue;
		dfs(v,u);
		x+=a[v];
	}
	a[u]=x;
}
void solve(){
	int n,m;cin>>n>>m;
	while(m--){
		int x,y,z;cin>>x>>y>>z;
		g[x].push_back(y);
		g[x].push_back(z);
		g[y].push_back(x);
		g[z].push_back(x);
		r[x]+=2;
	}	
	queue<int>q;
	vector<int>res;
	for(int i=1;i<=n;i++){
		if(r[i]==0){
			q.push(i);
			a[i]=1;
		}
	}
	while(q.size()){
		int p=q.front();q.pop();
		res.push_back(p);
		for(auto it:g[p]){
			r[it]--;
			if(r[it]==0)
				q.push(it);
		}
	}
	if(res.size()!=n){
		cout<<-1<<'\n';
	}else{
		int ans=0;
		dfs(1,-1);
		for(int i=1;i<=n;i++)
			ans+=a[i];
		if(ans>1e9){
			cout<<-1;
		}else
			cout<<ans;
	}
}
signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t=1;
	// cin>>t;
	while(t--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 53092kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 4ms
memory: 54128kb

input:

3 1
1 2 3

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 3ms
memory: 53568kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 6ms
memory: 54168kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #5:

score: 0
Accepted
time: 3ms
memory: 53232kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 4ms
memory: 54864kb

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #7:

score: 0
Accepted
time: 8ms
memory: 53680kb

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #8:

score: 0
Accepted
time: 0ms
memory: 54032kb

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #9:

score: 0
Accepted
time: 0ms
memory: 54072kb

input:

5 1
1 2 3

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 0ms
memory: 53992kb

input:

5 2
1 2 3
2 3 4

output:

8

result:

ok 1 number(s): "8"

Test #11:

score: 0
Accepted
time: 0ms
memory: 54524kb

input:

10 1
1 2 3

output:

11

result:

ok 1 number(s): "11"

Test #12:

score: 0
Accepted
time: 0ms
memory: 53180kb

input:

10 1
1 2 2

output:

11

result:

ok 1 number(s): "11"

Test #13:

score: 0
Accepted
time: 3ms
memory: 53492kb

input:

10 2
1 2 3
2 3 4

output:

13

result:

ok 1 number(s): "13"

Test #14:

score: 0
Accepted
time: 11ms
memory: 53344kb

input:

10 2
1 2 2
2 3 4

output:

14

result:

ok 1 number(s): "14"

Test #15:

score: -100
Wrong Answer
time: 3ms
memory: 52996kb

input:

10 3
1 2 3
1 8 8
2 3 3

output:

15

result:

wrong answer 1st numbers differ - expected: '13', found: '15'