QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#537951 | #6705. Median | XiaoretaW | TL | 0ms | 3784kb | C++20 | 2.2kb | 2024-08-30 20:03:08 | 2024-08-30 20:03:08 |
Judging History
answer
#include <bits/stdc++.h>
#define debug(...) 42;
#ifdef LOCAL
#include "debug.h"
#endif
using namespace std;
#define V vector
#define fi first
#define se second
#define mp make_pair
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define F(i, l, r) for (int i = l; i < r; ++i)
#define R(i, l, r) for (int i = r-1; i >= l; --i)
typedef long long LL;
typedef double DB;
typedef pair<int, int> PII;
template<typename T> bool setmin(T &a, T b) { return (a > b ? a = b, 1 : 0); }
template<typename T> bool setmax(T &a, T b) { return (a < b ? a = b, 1 : 0); }
// mt19937_64 rng {chrono::steady_clock::now().time_since_epoch().count()};
/* -------------Main code------------- */
void solve() {
int n, m; cin >> n >> m;
V<V<int>> g(n + 1), g_t(n + 1);
V<int> d(n + 1);
F(i, 0, m) {
int u, v; cin >> u >> v;
d[v]++;
g[u].emplace_back(v);
g_t[v].emplace_back(u);
}
auto has = [&]() {
V<int> q;
F(i, 1, n + 1) if (d[i] == 0) q.push_back(i);
for (int i = 0; i < sz(q); i++) {
int u = q[i];
for (int v : g[u]) if (--d[v] == 0) q.push_back(v);
}
for (int i = 1; i <= n; i++) if (d[i]) return true;
return false;
};
if (has()) {
cout << string(n, '0') << '\n';
return;
}
V<int> comp1(n + 1), comp2(n + 1), vis(n + 1);
int cnt1 = 0, cnt2 = 0;
auto dfs1 = [&](auto dfs1, int u) -> void {
++cnt1;
for (int v : g[u]) if (!vis[v]) dfs1(dfs1, v);
};
auto dfs2 = [&](auto dfs2, int u) -> void {
++cnt2;
for (int v : g_t[u]) if (!vis[v]) dfs2(dfs2, v);
};
for (int u = 1; u <= n; u++) {
vis.assign(n + 1, 0);
vis[u] = 1;
cnt1 = cnt2 = 0;
dfs1(dfs1, u);
dfs2(dfs2, u);
--cnt1; --cnt2;
comp1[u] = cnt1;
comp2[u] = cnt2;
}
for (int i = 1; i <= n; i++) {
if (comp1[i] <= n / 2 and comp2[i] <= n / 2) cout << 1;
else cout << 0;
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int tt; cin >> tt;
while (tt--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3784kb
input:
2 5 4 1 2 3 2 2 4 2 5 3 2 1 1 2 3
output:
01000 000
result:
ok 2 lines
Test #2:
score: -100
Time Limit Exceeded
input:
66 13 2 9 13 7 11 11 19 9 1 8 1 5 1 2 8 4 2 2 1 5 2 6 3 3 11 3 2 4 6 6 10 9 8 3 5 1 7 5 8 3 9 4 9 6 7 3 1 2 3 11 6 9 4 1 6 5 2 1 5 4 6 8 4 15 15 10 6 15 8 7 6 11 1 5 2 3 4 11 13 4 6 10 12 10 13 1 6 15 2 5 12 13 14 5 3 15 86 14 12 8 1 14 9 8 15 5 10 1 9 11 2 6 2 7 10 10 13 14 5 4 13 5 8 4 10 13 9 6 9...