QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#548075 | #8760. 不等式 | xfs001 | ML | 4ms | 16992kb | C++14 | 1.2kb | 2024-09-05 15:15:17 | 2024-09-05 15:15:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>g[202000];
vector<pair<int,int>>v[202000];
int r[2020000],a[202000];
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();
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();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 14516kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 15976kb
input:
3 1 1 2 3
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 0ms
memory: 15676kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 16576kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #5:
score: 0
Accepted
time: 0ms
memory: 14260kb
input:
3 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 4ms
memory: 15528kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #7:
score: 0
Accepted
time: 4ms
memory: 16992kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 3ms
memory: 15416kb
input:
5 1 1 2 2
output:
6
result:
ok 1 number(s): "6"
Test #9:
score: 0
Accepted
time: 3ms
memory: 14264kb
input:
5 1 1 2 3
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 0ms
memory: 15884kb
input:
5 2 1 2 3 2 3 4
output:
8
result:
ok 1 number(s): "8"
Test #11:
score: 0
Accepted
time: 3ms
memory: 15320kb
input:
10 1 1 2 3
output:
11
result:
ok 1 number(s): "11"
Test #12:
score: 0
Accepted
time: 0ms
memory: 15516kb
input:
10 1 1 2 2
output:
11
result:
ok 1 number(s): "11"
Test #13:
score: 0
Accepted
time: 3ms
memory: 16724kb
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: 15884kb
input:
10 2 1 2 2 2 3 4
output:
14
result:
ok 1 number(s): "14"
Test #15:
score: 0
Accepted
time: 2ms
memory: 15228kb
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: 16164kb
input:
20 1 1 2 2
output:
21
result:
ok 1 number(s): "21"
Test #17:
score: 0
Accepted
time: 3ms
memory: 15768kb
input:
20 2 1 2 3 2 3 3
output:
23
result:
ok 1 number(s): "23"
Test #18:
score: 0
Accepted
time: 3ms
memory: 16076kb
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: 0ms
memory: 16976kb
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