QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#548074#8760. 不等式xfs001ML 18ms102576kbC++141.2kb2024-09-05 15:14:252024-09-05 15:14:26

Judging History

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

  • [2024-09-05 15:14:26]
  • 评测
  • 测评结果:ML
  • 用时:18ms
  • 内存:102576kb
  • [2024-09-05 15:14:25]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>g[2020000];
vector<pair<int,int>>v[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];
	}
	for(auto it:v[u]){
		x=max(a[it.first]+a[it.second],x);
	}
	a[u]=x;
}
void solve(){
	int n,m,f=1;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);
		v[x].push_back({z,y});
		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;
		f=res.back();
		// cout<<f;
		for(int i=1;i<=n;i++){
			if(!a[i]){
				dfs(i,-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: 7ms
memory: 101260kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 18ms
memory: 101420kb

input:

3 1
1 2 3

output:

4

result:

ok 1 number(s): "4"

Test #3:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 7ms
memory: 102260kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #5:

score: 0
Accepted
time: 15ms
memory: 101248kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #7:

score: 0
Accepted
time: 7ms
memory: 100832kb

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #8:

score: 0
Accepted
time: 7ms
memory: 101168kb

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #9:

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

input:

5 1
1 2 3

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

5 2
1 2 3
2 3 4

output:

8

result:

ok 1 number(s): "8"

Test #11:

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

input:

10 1
1 2 3

output:

11

result:

ok 1 number(s): "11"

Test #12:

score: 0
Accepted
time: 17ms
memory: 100716kb

input:

10 1
1 2 2

output:

11

result:

ok 1 number(s): "11"

Test #13:

score: 0
Accepted
time: 7ms
memory: 101796kb

input:

10 2
1 2 3
2 3 4

output:

13

result:

ok 1 number(s): "13"

Test #14:

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

input:

10 2
1 2 2
2 3 4

output:

14

result:

ok 1 number(s): "14"

Test #15:

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

input:

10 3
1 2 3
1 8 8
2 3 3

output:

13

result:

ok 1 number(s): "13"

Test #16:

score: 0
Accepted
time: 7ms
memory: 102316kb

input:

20 1
1 2 2

output:

21

result:

ok 1 number(s): "21"

Test #17:

score: 0
Accepted
time: 13ms
memory: 101732kb

input:

20 2
1 2 3
2 3 3

output:

23

result:

ok 1 number(s): "23"

Test #18:

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

input:

20 3
7 14 6
1 2 3
4 7 20

output:

24

result:

ok 1 number(s): "24"

Test #19:

score: 0
Accepted
time: 13ms
memory: 101732kb

input:

20 4
1 2 2
6 10 6
2 3 3
3 4 5

output:

-1

result:

ok 1 number(s): "-1"

Test #20:

score: -100
Memory Limit Exceeded

input:

20 5
1 17 3
1 2 3
2 3 4
3 4 5
8 13 16

output:


result: