QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#612076 | #6705. Median | kevinyang# | AC ✓ | 3ms | 3712kb | C++14 | 3.2kb | 2024-10-05 04:26:28 | 2024-10-05 04:26:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
bool check(vector<vector<int>>adj, int n){
vector<bool>vis(n+1);
queue<int>q;
vector<int>deg(n+1);
for(int i = 1; i<=n; i++){
for(int j: adj[i]){
deg[j]++;
}
}
for(int i = 1; i<=n; i++){
if(deg[i] == 0){
q.push(i);
}
}
while(q.size()){
int cur = q.front(); q.pop();
vis[cur] = 1;
for(int nxt: adj[cur]){
deg[nxt]--;
if(deg[nxt] == 0){
q.push(nxt);
}
}
}
return count(vis.begin(),vis.end(),1) == n;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t--){
int n,m;
cin >> n >> m;
vector<vector<int>>adj(n+1);
vector<vector<int>>rev(n+1);
for(int i = 0; i<m; i++){
int x,y;
cin >> x >> y;
adj[x].push_back(y);
rev[y].push_back(x);
}
if(!check(adj,n)){
for(int i = 0; i<n; i++){
cout << "0";
}
cout << '\n';
continue;
}
vector<bitset<105>>dp(n+1);
vector<bitset<105>>dp2(n+1);
{
queue<int>q;
vector<int>deg(n+1);
for(int i = 1; i<=n; i++){
for(int j: adj[i]){
deg[j]++;
}
}
for(int i = 1; i<=n; i++){
if(deg[i] == 0){
q.push(i);
}
dp[i][i] = 1;
}
while(q.size()){
int cur = q.front(); q.pop();
for(int nxt: adj[cur]){
deg[nxt]--;
dp[nxt] |= dp[cur];
if(deg[nxt] == 0){
q.push(nxt);
}
}
}
}
{
queue<int>q;
vector<int>deg(n+1);
for(int i = 1; i<=n; i++){
for(int j: rev[i]){
deg[j]++;
}
}
for(int i = 1; i<=n; i++){
if(deg[i] == 0){
q.push(i);
}
dp2[i][i] = 1;
}
while(q.size()){
int cur = q.front(); q.pop();
for(int nxt: rev[cur]){
deg[nxt]--;
dp2[nxt] |= dp2[cur];
if(deg[nxt] == 0){
q.push(nxt);
}
}
}
}
for(int i = 1; i<=n; i++){
int c1 = dp[i].count();
int c2 = dp2[i].count();
if(c1 <= (n+1)/2 && c2 <= (n+1)/2){
cout << 1;
}
else{
cout << 0;
}
}
cout << '\n';
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: 0
Accepted
time: 3ms
memory: 3712kb
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...
output:
1111111111111 01001000111 111 11111111111 111111111111111 001001000000000 00100 01100 1111111 1000000000000 111101101 111111111 000011111011101 010111111 001100000 0100001001101 1111111111111 001000010000000 10010111011 001000000000100 11111111111 00100000011 11111 01000000110 11101110111 00000 1111...
result:
ok 66 lines