QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#614525 | #8760. 不等式 | wdw | WA | 0ms | 3836kb | C++20 | 1.7kb | 2024-10-05 16:31:25 | 2024-10-05 16:31:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define int long long
const int N = 2e5 + 5;
#define endl '\n'
vector<vector<int>> q1, q2;
int vis[N];
int f = 0;
void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < q2[u].size(); i++) {
int y = q2[u][i];
if (vis[y]) {
f = 1;
return;
}
dfs(y);
}
}
int in[N];
int w[N];
void solve() {
int n, m;
cin >> n >> m;
q1 = vector<vector<int>>(n + 5);
q2 = vector<vector<int>>(n + 5);
for (int i = 1, a, b, c; i <= m; i++) {
cin >> a >> b >> c;
q1[b].push_back(a);
q1[c].push_back(a);
q2[a].push_back(b);
q2[a].push_back(c);
in[a] += 2;
}
for (int i = 1; i <= n; i++) {
if (vis[i])continue;
dfs(i);
}
if (f) {
cout << -1 << '\n';
return;
}
queue<int>q;
int ans=0;
for (int i = 1; i <= n;i++){
if(in[i]==0){q.push(i);w[i]=1;}
}
while(!q.empty()){
int y=q.front();
q.pop();
for(int i=0;i<q1[y].size();i++){
int y1=q1[y][i];
in[y1]--;
w[y1]+=w[y];
if(!in[y1]){
q.push(y1);
}
}
}
for(int i=1;i<=n;i++){
// cout<<w[i]<<" ";
ans+=w[i];
}
if(ans<=1e9){
cout<<ans<<'\n';
}else{
cout<<-1<<'\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
//cout << fixed << setprecision(5);
int T = 1;
//cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3836kb
input:
3 1 1 2 2
output:
-1
result:
wrong answer 1st numbers differ - expected: '4', found: '-1'