QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#756130 | #8760. 不等式 | ckbox | WA | 2ms | 10480kb | C++14 | 1.8kb | 2024-11-16 19:07:07 | 2024-11-16 19:07:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200005;
vector<int> adj[N];
int ind[N];
ll ans[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;++i){
int x,y,z;
cin >> x >> y >> z;
adj[x].push_back(y);
adj[x].push_back(z);
ind[y]++;
ind[z]++;
}
queue<int> q;
for(int i = 1;i <= n;++i){
if(!ind[i])q.push(i);
}
vector<int> tp;
int t = 1;
while(q.size()){
int nn = q.size();
while(nn--){
int r = q.front();
tp.push_back(r);
q.pop();
for(int y : adj[r]){
if(!--ind[y]){
q.push(y);
}
}
}
t++;
}
for(int i = 1;i <= n;++i){
if(ind[i]){
cout << -1 << '\n';
return 0;
}
}
for(int i = tp.size() - 1;i >= 0;--i){
int ii = tp[i];
if(adj[ii].size() == 0)ans[ii] = 1;
else{
ll m1 = 0,m2 = 0;
for(int y : adj[ii]){
if(ans[y] > m1){
m2 = m1;
m1 = ans[y];
}else if(ans[y] == m1){
m2 = ans[y];
}
}
ans[ii] = m1 + m2;
}
}
// for(int i = 1;i <= n;++i){
// cerr << ans[i] << ' ';
// }
ll sum = 0;
for(int i = 1;i <= n;++i){
sum += ans[i];
}
cout << (sum > 1e9 ? -1 : sum) << '\n';
return 0;
}
/*
o
/ \
o o
\ /
o
x -> y <=> x > y
1 2 1 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9320kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 2ms
memory: 8700kb
input:
3 1 1 2 3
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 1ms
memory: 8584kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 9144kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #5:
score: 0
Accepted
time: 0ms
memory: 9728kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 1ms
memory: 9388kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #7:
score: 0
Accepted
time: 1ms
memory: 9504kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 2ms
memory: 10128kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #9:
score: 0
Accepted
time: 1ms
memory: 10480kb
input:
5 1 1 2 3
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: -100
Wrong Answer
time: 1ms
memory: 8788kb
input:
5 2 1 2 3 2 3 4
output:
7
result:
wrong answer 1st numbers differ - expected: '8', found: '7'