QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#696070 | #8760. 不等式 | qinglu09# | WA | 1ms | 3620kb | C++23 | 1.9kb | 2024-10-31 21:27:45 | 2024-10-31 21:28:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ll, ll> PLL;
#define endl '\n'
#define rep(i, a, b) for(ll i = (a); i <= (b); i++)
#define per(i, a, b) for(ll i = (a); i >= (b); i--)
#define debug(x) cout<<#x<<": "<<x<<endl
const ll mod=998244353;
const int N=2e5+10;
typedef pair<int,int>PII;
int n;
vector<int> e[N],tp;//e为终点,tp为路径
int c[N];
bool dfs(int x)
{
c[x]=-1;
for(auto p:e[x])
{
//dfn[p]=max(dfn[p],dfn[x]+1);
if(c[p]<0) return 0;
else if(!c[p])
{
if(!dfs(p)) return 0;
}
}
c[x]=1;
tp.push_back(x);
return 1;
}
bool toposort()
{
for(int i=1;i<=n;i++)
{
if(!c[i])
{
//dfn[i]=0;
if(!dfs(i)) return 0;
}
}
reverse(tp.begin(),tp.end());
return 1;
}
void solve()
{
cin>>n;
int m;
cin>>m;
vector<PII>path[n+1];
ll num[n+1];
memset(num,0,sizeof(num));
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
e[x].push_back(z);
e[y].push_back(z);
path[z].push_back({x,y});
}
if(!toposort())
{
cout<<-1<<endl;
return;
}
ll sum=0;
for(auto p:tp)
{
for(auto [x,y]:path[p])
{
num[p]=max(num[p],num[x]+num[y]);
}
if(num[p]==0) num[p]=1;
sum+=num[p];
if(sum>1e9)
{
cout<<-1<<endl;
return;
}
}
cout<<sum<<endl;
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
// #endif
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: 1ms
memory: 3620kb
input:
3 1 1 2 2
output:
-1
result:
wrong answer 1st numbers differ - expected: '4', found: '-1'