社区讨论
良好码风,求调~~~
P10935银河参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mhjrdhej
- 此快照首次捕获于
- 2025/11/04 07:15 4 个月前
- 此快照最后确认于
- 2025/11/04 07:15 4 个月前
CPP
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 100010, M = 300010;
typedef long long LL;
int n, m;
int h[N], hs[N], e[M], ne[M], w[M], idx;
bool in_stk[N];
int dist[N], Size[N];
int stk[N], top;
int dfn[N], low[N], timestamp;
int id[N], scc_cnt;
void add(int h[], int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++ timestamp;
stk[++ top] = u; in_stk[u] = true;
for(int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if(!dfn[j])
{
tarjan(j);
low[u] = min(low[u], low[j]);
}
else if(in_stk[j]) low[u] = min(low[u], dfn[j]);
}
if(dfn[u] == low[u])
{
int y;
++ scc_cnt;
do
{
y = stk[top --];
in_stk[y] = false;
id[y] = scc_cnt;
Size[scc_cnt] ++;
}while(y != u);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(h, -1, sizeof h);
memset(hs, -1, sizeof hs);
cin >> n >> m;
for(int i = 1; i <= n; i ++) add(h, 0, i, 1);
while(m --)
{
int t, a, b;
cin >> t >> a >> b;
if(t == 1) add(h, a, b, 0), add(h, b, a, 0);
else if(t == 2) add(h, a, b, 1);
else if(t == 3) add(h, b, a, 0);
else if(t == 4) add(h, b, a, 1);
else add(h, a, b, 0);
}
tarjan(0);
bool success = true;
for(int i = 0; i <= n; i ++)
{
for(int j = h[i]; j != -1; j = ne[j])
{
int k = e[j];
int a = id[i], b = id[k];
if(a == b)
{
if(w[j] > 0)
{
success = false;
break ;
}
}
else
{
add(hs, a, b, w[j]);
}
}
if(!success) break ;
}
if(!success) puts("-1");
else
{
for(int i = scc_cnt; i; i --)
for(int j = hs[i]; j != -1; j = ne[j])
{
int k = e[j];
dist[k] = max(dist[k], dist[i] + w[j]);
}
LL res = 0;
for(int i = 1; i <= scc_cnt; i ++)
res += (LL)Size[i] * dist[i];
cout << res << endl;
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...