QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#785975 | #4957. Helping the Transit | Yarema# | WA | 1ms | 3768kb | C++20 | 2.2kb | 2024-11-26 19:46:00 | 2024-11-26 19:46:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;
struct DSU
{
int n;
VI p, sz;
DSU(int _n = 0)
{
n = _n;
p.resize(n);
iota(ALL(p), 0);
sz.assign(n, 1);
}
int find(int v)
{
if (v == p[v])
return v;
return p[v] = find(p[v]);
}
bool unite(int u, int v)
{
u = find(u);
v = find(v);
if (u == v)
return false;
if (sz[u] > sz[v])
swap(u, v);
p[u] = v;
sz[v] += sz[u];
return true;
}
};
const int N = 100'447;
int in[N];
int out[N];
VI g[N];
VI gr[N];
bool used[N];
int col[N];
VI tps;
void dfs1(int v)
{
used[v] = 1;
for (auto to : g[v])
if (!used[to])
dfs1(to);
tps.PB(v);
}
void dfs2(int v, int c)
{
used[v] = 1;
col[v] = c;
for (auto to : gr[v])
if (!used[to])
dfs2(to, c);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
DSU d(n);
vector<PII> edges(m);
FOR (i, 0, m)
{
int a, b;
cin >> a >> b;
a--, b--;
edges[i] = {a, b};
g[a].PB(b);
gr[b].PB(a);
}
FOR (i, 0, n)
if (!used[i])
dfs1(i);
reverse(ALL(tps));
int c = 0;
fill(used, used + n, false);
for (auto v : tps)
if (!used[v])
dfs2(v, c++);
for (auto [a, b] : edges)
{
if (col[a] != col[b])
{
out[col[a]]++;
in[col[b]]++;
}
d.unite(col[a], col[b]);
}
if (c == 1)
{
cout << 0 << '\n';
return 0;
}
//set<int> s;
//FOR (i, 0, c)
// s.insert(d.find(i));
//int sz = SZ(s);
//int ans = 0;
//int x = 0;
//s.clear();
//FOR (i, 0, c)
//{
// ans += out[i] == 0;
// if (in[i] == 0)
// s.insert(d.find(i));
// x += in[i] == 0;
//}
//ans += x + sz - SZ(s);
//if (x == 1)
// ans--;
//cout << ans << '\n';
int ans = 0;
FOR (i, 0, c)
{
ans += out[i] == 0;
ans += in[i] == 0;
}
cout << ans - 1 << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3768kb
input:
7 7 1 2 2 3 3 1 6 1 6 4 4 5 7 6
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
7 7 2 1 3 2 1 3 1 6 4 6 5 4 6 7
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
2 1 1 2
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3620kb
input:
3 3 1 2 2 3 3 1
output:
0
result:
ok single line: '0'
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3492kb
input:
2 0
output:
3
result:
wrong answer 1st lines differ - expected: '2', found: '3'