QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#669754#8760. 不等式zwu2021016689WA 0ms12052kbC++171008b2024-10-23 19:34:142024-10-23 19:34:15

Judging History

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

  • [2024-10-23 19:34:15]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:12052kb
  • [2024-10-23 19:34:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define dg(x) cout<<#x<<'='<<x<<'\n';

const int N=2e5+10;

vector<int>g[N];
int vis[N],dis[N],in[N];
int co=0;
int ans=0;
int root;
set<int>s;

void dfs(int x){
	//dg(x)
	if(co==1) return ;
	if(s.find(x)!=s.end()){
		co=1;
		return ;
	}
	s.insert(x);
	if(vis[x]){
		return ;
	}
	vis[x]=1;
	for(auto u:g[x]){
		dfs(u);
	}
	if(g[x].size()==0){
		dis[x]=1;
	}
	else{
		for(auto u:g[x]) dis[x]+=dis[u];
	}
}

inline void solve()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
		g[x].push_back(y);
		g[x].push_back(z);
	}
	
	for(int i=1;i<=n;i++){
		if(vis[i]) continue;
		
		dfs(i);
		s.clear();
	}
	for(int i=1;i<=n;i++) ans+=dis[i];
	if(co) ans=-1;
	cout<<ans<<'\n';
}
/*
5 2
1 2 3
3 1 2

6 3
1 2 3
3 4 5
6 3 5
*/

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 12052kb

input:

3 1
1 2 2

output:

-1

result:

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