QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#786000 | #4957. Helping the Transit | Yarema# | WA | 1ms | 5660kb | C++20 | 2.0kb | 2024-11-26 19:51:36 | 2024-11-26 19:52:03 |
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;
}
int ans = 0;
int cnt = 0;
FOR (i, 0, c)
{
ans += out[i] == 0;
ans += in[i] == 0;
cnt += in[i] == 0 && out[i] == 0;
}
if (cnt * 2 == ans)
cout << cnt << '\n';
else
cout << ans - cnt - 1 << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5660kb
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: 3656kb
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: 3624kb
input:
2 1 1 2
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
3 3 1 2 2 3 3 1
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
2 0
output:
2
result:
ok single line: '2'
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3564kb
input:
6 4 1 2 1 3 4 6 5 6
output:
5
result:
wrong answer 1st lines differ - expected: '3', found: '5'