QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#785954 | #4957. Helping the Transit | WeaRD276# | AC ✓ | 9ms | 5580kb | C++20 | 2.9kb | 2024-11-26 19:42:01 | 2024-11-26 19:42:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define x first
#define y second
#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--)
typedef long long ll;
typedef double db;
typedef long double LD;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef pair<ll, ll> pll;
const int N = 1e4 + 47;
vector<int> g[N];
bool vis[N];
void dfs(int v, vector<int> &output)
{
vis[v] = 1;
for (int u: g[v])
{
if (!vis[u])
dfs(u, output);
}
output.pb(v);
}
void dfs(int v, vector<vector<int>> const& adj, vector<int> &output)
{
vis[v] = true;
for (auto u : adj[v])
if (!vis[u])
dfs(u, adj, output);
output.push_back(v);
}
void scc(int n, vector<vector<int>> &components, vector<vector<int>> &adj_cond)
{
fill(vis, vis + n, false);
components.clear(), adj_cond.clear();
vector<int> order;
for (int i = 0; i < n; i++)
if (!vis[i])
dfs(i, order);
vector<vector<int>> adj_rev(n);
FOR (v, 0, n)
{
for (int u : g[v])
adj_rev[u].push_back(v);
}
fill(vis, vis + n, false);
reverse(all(order));
vector<int> roots(n, 0);
for (auto v : order)
if (!vis[v]) {
vector<int> component;
dfs(v, adj_rev, component);
components.push_back(component);
int root = *min_element(begin(component), end(component));
for (auto u : component)
roots[u] = root;
}
adj_cond.assign(n, {});
for (int v = 0; v < n; v++)
for (auto u : g[v])
if (roots[v] != roots[u])
adj_cond[roots[v]].push_back(roots[u]);
}
int solve()
{
int n, m;
if (!(cin >> n >> m))
return 1;
fill(g, g + N, vector<int>());
FOR (i, 0, m)
{
int s, d;
cin >> s >> d;
--s;
--d;
g[s].pb(d);
}
vector<vector<int>> comps, adj, rev;
scc(n, comps, adj);
int nn = sz(comps);
if (nn == 1)
{
cout << "0\n";
return 0;
}
vector<bool> in(n), out(n);
FOR (i, 0, n)
{
//cerr << "i = " << i << '\n';
for (int to: adj[i])
{
//cerr << to << ' ';
in[i] = 1;
out[to] = 1;
}
//cerr << '\n';
}
int ins = count(all(in), true);
int outs = count(all(out), true);
int ans = nn - min(ins, outs);
cout << ans << '\n';
return 0;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int TET = 1e9;
//cin >> TET;
for (int i = 1; i <= TET; i++)
{
if (solve())
{
break;
}
#ifdef ONPC
cerr << "_____________________________\n";
#endif
}
#ifdef ONPC
cerr << "\nfinished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3880kb
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: 3836kb
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: 3840kb
input:
2 1 1 2
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
3 3 1 2 2 3 3 1
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
2 0
output:
2
result:
ok single line: '2'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3720kb
input:
6 4 1 2 1 3 4 6 5 6
output:
3
result:
ok single line: '3'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3996kb
input:
10 14 5 8 3 9 1 10 2 8 2 9 7 10 6 9 6 10 2 7 3 10 5 10 1 7 1 5 4 8
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3780kb
input:
10 5 3 8 2 7 1 6 4 9 5 10
output:
5
result:
ok single line: '5'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3792kb
input:
100 188 28 35 47 48 25 46 40 49 77 94 73 77 84 92 26 36 64 83 70 97 65 100 5 37 88 98 78 82 16 42 54 71 73 91 79 88 93 96 39 43 31 37 44 50 3 25 74 87 73 83 13 26 13 25 49 61 76 94 52 75 62 73 8 18 7 26 25 38 37 47 62 87 76 86 11 21 66 71 17 36 95 98 33 50 20 24 43 47 36 41 53 87 61 69 51 96 88 96 6...
output:
35
result:
ok single line: '35'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3864kb
input:
100 205 70 78 17 31 19 84 73 75 56 61 12 75 60 94 91 93 36 95 46 89 58 77 72 87 61 100 56 72 76 97 77 89 18 79 37 51 5 12 18 51 39 64 84 92 63 73 32 93 29 64 63 72 42 91 73 82 79 88 90 91 23 99 33 81 10 23 34 90 1 37 8 99 82 87 36 90 68 90 26 98 27 31 14 40 70 71 8 74 17 75 3 83 43 50 85 87 82 96 91...
output:
34
result:
ok single line: '34'
Test #11:
score: 0
Accepted
time: 1ms
memory: 4060kb
input:
100 188 47 54 66 90 81 98 65 82 9 38 48 52 11 20 95 99 84 89 26 32 53 93 72 94 44 57 31 37 51 87 83 97 60 90 31 42 43 72 38 40 55 72 30 35 90 99 49 59 34 36 70 93 28 36 68 88 75 91 59 69 16 38 6 42 12 18 73 84 88 93 49 72 5 19 10 23 18 27 26 30 74 81 80 97 47 52 14 25 93 97 14 29 82 93 6 34 8 26 96 ...
output:
35
result:
ok single line: '35'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
150 369 33 36 56 81 69 96 47 104 89 105 57 68 37 54 117 132 102 107 112 125 105 139 6 9 57 78 11 18 129 145 10 52 91 104 139 143 77 88 59 101 26 69 120 134 48 52 111 129 79 97 24 27 69 82 96 101 105 133 80 88 110 139 73 99 13 54 91 100 43 70 39 91 127 135 34 92 71 99 40 78 5 97 64 70 112 133 58 76 5...
output:
39
result:
ok single line: '39'
Test #13:
score: 0
Accepted
time: 1ms
memory: 3872kb
input:
150 436 82 86 8 63 144 150 84 112 132 137 50 89 109 120 12 71 93 116 59 82 126 144 70 122 132 138 124 145 77 107 107 109 80 94 56 79 24 63 8 103 123 144 60 111 46 54 86 89 141 145 40 96 6 94 62 122 121 124 11 78 31 34 36 115 92 102 103 122 51 65 1 19 25 71 98 100 52 96 123 136 83 102 24 32 137 150 1...
output:
38
result:
ok single line: '38'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3760kb
input:
150 506 56 109 102 114 91 102 122 132 101 114 132 150 109 116 29 32 9 53 61 78 140 150 30 66 22 93 127 139 45 71 116 141 64 107 64 69 19 91 64 109 66 107 78 100 68 94 28 88 115 139 116 136 47 96 98 113 93 114 60 90 108 110 55 108 41 63 101 116 77 94 24 81 131 149 79 86 23 83 60 109 141 146 3 79 132 ...
output:
34
result:
ok single line: '34'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3772kb
input:
200 1124 75 185 11 175 18 104 71 126 190 200 103 149 139 175 82 135 189 193 3 30 139 178 93 112 14 177 6 137 127 180 40 170 153 180 119 156 69 100 69 117 104 190 96 198 105 133 123 173 41 93 170 191 50 84 158 192 50 196 74 120 180 197 170 175 117 142 87 131 47 53 103 108 183 189 23 64 40 180 4 14 89...
output:
33
result:
ok single line: '33'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
200 400 147 163 187 197 175 194 167 169 6 16 13 40 171 183 33 72 104 179 100 182 24 74 144 167 13 69 180 193 22 95 30 53 64 78 163 196 119 195 158 197 5 30 158 189 153 173 77 93 180 194 138 167 99 121 70 82 117 172 147 186 6 70 42 83 124 184 89 95 174 200 37 91 181 184 73 78 153 182 1 19 106 134 183...
output:
63
result:
ok single line: '63'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
200 385 65 96 15 79 154 168 88 122 197 198 15 153 138 191 56 180 195 197 43 182 80 136 52 71 130 178 76 104 165 180 128 131 67 170 86 89 142 175 4 181 96 179 167 189 105 127 79 148 39 111 32 145 191 197 23 96 109 161 112 169 171 194 102 106 185 191 133 180 41 49 82 85 24 57 89 122 108 165 68 144 195...
output:
71
result:
ok single line: '71'
Test #18:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
200 388 21 98 111 118 37 132 105 127 101 123 126 130 30 105 9 86 57 85 91 103 98 126 194 199 157 183 8 23 146 193 59 67 134 135 167 183 148 176 166 188 3 71 41 78 95 102 19 110 181 185 157 189 157 163 95 129 178 182 136 138 56 64 72 123 116 135 141 159 177 184 123 126 77 131 68 125 64 138 130 136 92...
output:
67
result:
ok single line: '67'
Test #19:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
200 199 177 178 62 63 6 7 52 53 58 59 189 190 179 180 93 94 109 110 81 82 152 153 80 81 119 120 108 109 74 75 86 87 163 164 135 136 23 24 37 38 190 191 114 115 63 64 178 179 196 197 133 134 174 175 12 13 73 74 105 106 145 146 97 98 128 129 71 72 78 79 89 90 29 30 7 8 194 195 130 131 129 130 184 185 ...
output:
1
result:
ok single line: '1'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3916kb
input:
300 1368 65 162 93 259 90 236 104 216 148 193 48 205 198 255 40 120 205 298 115 182 241 267 38 112 98 243 69 234 136 235 86 245 21 232 113 156 233 237 227 243 184 293 143 170 184 258 292 299 130 205 153 213 229 242 181 287 232 272 81 257 127 144 201 232 220 247 271 285 159 234 129 194 53 73 194 254 ...
output:
46
result:
ok single line: '46'
Test #21:
score: 0
Accepted
time: 1ms
memory: 3920kb
input:
300 1325 199 288 280 298 257 289 197 263 281 291 247 267 246 289 247 284 92 112 238 299 22 152 116 293 122 218 58 228 83 226 199 216 47 116 94 268 82 176 200 293 9 12 274 279 198 246 141 204 289 297 130 211 211 260 66 211 139 166 146 223 54 207 268 285 218 291 286 300 162 169 261 279 182 231 150 170...
output:
49
result:
ok single line: '49'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3824kb
input:
300 1337 46 68 197 224 38 72 128 234 120 270 188 198 239 266 34 73 165 200 221 256 46 75 57 75 171 262 51 68 287 293 58 74 65 73 190 226 193 222 288 297 28 43 249 255 138 294 201 292 264 292 176 209 139 249 162 266 67 72 282 292 233 295 3 37 263 286 286 299 32 68 204 228 187 220 185 245 206 216 105 ...
output:
51
result:
ok single line: '51'
Test #23:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
350 1436 99 349 122 216 276 311 225 247 283 331 157 208 12 255 291 339 250 276 215 316 282 334 35 189 12 323 49 117 22 178 313 322 199 321 180 284 117 201 130 226 267 347 31 153 107 339 274 337 164 253 253 282 38 144 119 164 148 211 273 310 109 245 156 301 16 207 32 256 141 240 44 81 147 164 271 346...
output:
71
result:
ok single line: '71'
Test #24:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
400 1730 113 128 174 219 226 362 3 41 23 82 352 368 219 325 201 376 148 239 135 292 26 74 9 82 52 97 131 383 93 99 332 385 159 245 298 385 332 391 146 313 81 88 270 368 381 399 83 85 113 117 96 113 302 367 46 61 147 259 204 211 81 124 133 228 248 331 359 382 302 343 304 358 182 260 258 386 19 51 184...
output:
66
result:
ok single line: '66'
Test #25:
score: 0
Accepted
time: 1ms
memory: 4168kb
input:
450 1994 416 436 325 348 166 187 14 261 292 322 34 311 53 168 283 349 202 331 64 189 8 200 266 334 12 359 28 157 269 345 259 310 437 448 365 413 383 432 313 344 266 315 123 170 434 448 325 350 283 286 73 230 252 320 6 122 205 241 95 340 213 326 157 195 92 157 97 218 26 219 182 239 367 392 385 419 15...
output:
81
result:
ok single line: '81'
Test #26:
score: 0
Accepted
time: 1ms
memory: 3868kb
input:
300 2336 181 237 86 165 284 297 287 293 242 266 194 287 22 219 201 259 38 84 163 263 108 277 79 166 35 217 103 209 285 296 214 233 149 289 166 287 175 224 11 112 130 289 243 259 288 292 41 85 192 200 168 265 198 200 236 261 83 107 266 299 89 261 78 177 130 196 283 298 106 238 221 255 38 205 243 296 ...
output:
34
result:
ok single line: '34'
Test #27:
score: 0
Accepted
time: 1ms
memory: 4196kb
input:
500 3411 472 483 484 497 330 346 448 487 79 102 64 246 101 142 387 460 278 307 268 328 384 499 404 472 312 349 425 460 111 230 396 435 36 187 337 338 324 345 461 494 336 339 56 86 387 425 103 338 131 182 204 207 101 337 340 354 423 457 322 333 340 348 386 414 416 430 474 495 175 309 94 189 345 351 4...
output:
63
result:
ok single line: '63'
Test #28:
score: 0
Accepted
time: 0ms
memory: 4120kb
input:
500 250 79 329 26 276 92 342 229 479 63 313 18 268 30 280 72 322 168 418 116 366 240 490 203 453 103 353 172 422 117 367 52 302 25 275 38 288 10 260 184 434 108 358 46 296 159 409 61 311 102 352 80 330 228 478 187 437 99 349 155 405 136 386 32 282 149 399 56 306 207 457 248 498 218 468 127 377 131 3...
output:
250
result:
ok single line: '250'
Test #29:
score: 0
Accepted
time: 1ms
memory: 3800kb
input:
500 250 11 261 101 351 247 497 197 447 141 391 205 455 75 325 164 414 22 272 112 362 95 345 70 320 96 346 50 300 241 491 20 270 171 421 59 309 49 299 184 434 104 354 161 411 107 357 17 267 122 372 182 432 224 474 168 418 61 311 149 399 108 358 85 335 204 454 24 274 47 297 89 339 26 276 174 424 124 3...
output:
250
result:
ok single line: '250'
Test #30:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
500 250 113 363 73 323 104 354 49 299 208 458 67 317 135 385 36 286 164 414 223 473 247 497 165 415 191 441 226 476 94 344 180 430 70 320 206 456 87 337 175 425 221 471 98 348 12 262 205 455 102 352 220 470 2 252 155 405 129 379 203 453 184 434 229 479 250 500 18 268 86 336 109 359 224 474 151 401 4...
output:
250
result:
ok single line: '250'
Test #31:
score: 0
Accepted
time: 1ms
memory: 3912kb
input:
500 499 447 448 182 183 241 242 423 424 316 317 134 135 488 489 40 41 246 247 8 9 206 207 95 96 141 142 287 288 310 311 318 319 111 112 424 425 116 117 184 185 186 187 291 292 122 123 235 236 270 271 358 359 67 68 489 490 78 79 227 228 481 482 455 456 225 226 283 284 430 431 57 58 288 289 24 25 250 ...
output:
1
result:
ok single line: '1'
Test #32:
score: 0
Accepted
time: 0ms
memory: 4104kb
input:
500 498 447 449 393 394 333 335 283 284 107 108 87 89 169 170 59 60 467 469 489 490 197 199 51 53 359 361 69 71 381 383 195 196 379 380 443 444 97 99 243 244 29 31 133 135 227 228 427 428 103 105 181 183 463 465 71 72 275 276 191 192 367 368 145 147 173 174 449 451 297 299 227 229 281 283 67 69 407 ...
output:
251
result:
ok single line: '251'
Test #33:
score: 0
Accepted
time: 1ms
memory: 4016kb
input:
1000 4516 305 882 105 476 234 269 629 835 31 475 900 909 317 469 601 818 996 998 95 883 867 974 555 715 804 846 781 803 485 851 605 894 8 765 75 601 826 923 789 803 386 717 548 921 525 967 942 963 601 604 700 717 881 927 766 907 603 744 762 941 911 942 973 999 183 780 106 507 474 795 682 980 651 670...
output:
191
result:
ok single line: '191'
Test #34:
score: 0
Accepted
time: 1ms
memory: 4232kb
input:
1000 4324 857 862 766 796 1 171 316 338 356 851 88 307 797 874 854 871 735 795 60 590 442 927 797 891 980 996 662 974 64 771 759 810 256 850 965 971 974 982 591 846 210 602 679 898 121 719 624 635 951 977 520 876 700 779 230 312 448 966 762 789 864 913 814 942 608 908 250 541 525 683 534 997 383 841...
output:
172
result:
ok single line: '172'
Test #35:
score: 0
Accepted
time: 2ms
memory: 5116kb
input:
5000 22778 2371 4944 1549 2092 1405 2426 2670 4784 4541 4756 3447 3514 3241 4587 3609 4009 685 1912 3176 3817 1432 2734 1699 2641 2802 4679 907 2624 3543 3727 203 2840 4659 4993 3924 4639 2655 3295 1949 2765 3695 4482 1452 3321 1828 4028 4421 4856 3345 4379 2567 4666 2893 4716 4282 4746 4480 4766 10...
output:
893
result:
ok single line: '893'
Test #36:
score: 0
Accepted
time: 5ms
memory: 5104kb
input:
5000 22507 4887 4959 809 4741 2152 2851 4013 4154 1091 3693 3264 3309 4450 4589 4772 4956 3440 3577 402 1344 1946 2016 1024 1873 3991 4817 3461 3605 488 3857 4912 4992 4059 4618 1947 3341 1059 1066 3038 3461 2798 3995 3019 4930 3448 3720 1921 4625 3090 3422 315 4290 3918 4679 4388 4594 1549 4209 241...
output:
892
result:
ok single line: '892'
Test #37:
score: 0
Accepted
time: 9ms
memory: 5580kb
input:
10000 45473 5810 8522 7832 7864 7213 9034 8198 9199 6768 8753 9110 9526 9977 9994 3746 7074 5537 9163 6773 9798 9520 9846 8847 9419 7676 8011 9968 9969 5847 6887 7338 8958 1362 7424 2755 4966 3467 9096 8147 9685 8354 8853 9759 9799 2186 3250 8784 9384 6828 9251 8861 9317 2214 7008 4748 5691 1484 271...
output:
1803
result:
ok single line: '1803'