QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#614715 | #8760. 不等式 | wdw | WA | 1ms | 5684kb | C++20 | 2.2kb | 2024-10-05 16:50:58 | 2024-10-05 16:51:02 |
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, int x) {
vis[u] = 1;
for (int i = 0; i < q2[u].size(); i++) {
int y = q2[u][i];
if (y == x) {
f = 1;
return;
}
if (vis[y])continue;
dfs(y, x);
}
}
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);
map<pair<int, int>, int> mp1, mp2;
for (int i = 1, a, b, c; i <= m; i++) {
cin >> a >> b >> c;
if (mp1[{b, a}] == 0) {
q1[b].push_back(a);
mp1[{b, a}] = c;
}
if (mp1[{c, a}] == 0) {
q1[c].push_back(a);
mp1[{c, a}] = b;
}
if (mp2[{a, b}] == 0) {
q2[a].push_back(b);
mp2[{a, b}] = 1;
in[a]++;
}
if (mp2[{a, c}] == 0) {
q2[a].push_back(c);
mp2[{a, c}] = 1;
in[a]++;
}
}
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];
// cout<<y1<<" "<<y<<"\n";
in[y1]--;
w[y1] = max(w[y1], (w[y] + w[mp1[{y, y1}]]));
if (!in[y1]) {
q.push(y1);
}
}
}
for (int i = 1; i <= n; i++) {
// cout<<w[i]<<" ";
if(w[i]==0)f=1;
ans += w[i];
}
if(f){
cout << -1 << '\n';
return;
}
if (ans <= 1e9) {
cout << ans << '\n';
} else {
cout << -1 << '\n';
}
// cout<<ans<<'\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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5684kb
input:
3 1 1 2 3
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5616kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #7:
score: 0
Accepted
time: 0ms
memory: 5620kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
5 1 1 2 3
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3572kb
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: 3636kb
input:
10 1 1 2 3
output:
11
result:
ok 1 number(s): "11"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
10 1 1 2 2
output:
11
result:
ok 1 number(s): "11"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
10 2 1 2 3 2 3 4
output:
13
result:
ok 1 number(s): "13"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
10 2 1 2 2 2 3 4
output:
14
result:
ok 1 number(s): "14"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3872kb
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: 0ms
memory: 3576kb
input:
20 1 1 2 2
output:
21
result:
ok 1 number(s): "21"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
20 2 1 2 3 2 3 3
output:
23
result:
ok 1 number(s): "23"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
20 3 7 14 6 1 2 3 4 7 20
output:
24
result:
ok 1 number(s): "24"
Test #19:
score: -100
Wrong Answer
time: 0ms
memory: 3672kb
input:
20 4 1 2 2 6 10 6 2 3 3 3 4 5
output:
31
result:
wrong answer 1st numbers differ - expected: '-1', found: '31'