QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#593631 | #6705. Median | PonyHex | AC ✓ | 3ms | 3888kb | C++20 | 4.0kb | 2024-09-27 15:09:16 | 2024-09-27 15:09:16 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define ll long long
//#define double long double
#define lc u<<1
#define rc u<<1|1
#define X first
#define Y second
#define endl "\n"
#define int long long
const int N = 2e3 + 50;
const int M = 5e5 + 5;
const ll maxm = 1e18 + 5;
const ll mod = 998244353;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
//random_shuffle(id + 1, id + n + 1);
ll ksm(ll a, ll b);
ll gcd(ll a, ll b);
template<class T>inline void read(T& x) {
x = 0;
char c = getchar();
while (!isdigit(c))c = getchar();
while (isdigit(c))x = x * 10 + (c & 15), c = getchar();
}
void write(ll x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
return;
}
bool f[N];
vector<pair<ll, ll> >op;
vector<vector<ll> >e(N);
int din[N];
set<ll>s;
ll n, m;
bool topo() {
queue<ll>q;
for (auto p = s.begin(); p != s.end(); p++) {
if (din[*p] == 0)q.push(*p);
}
ll idx = 0;
while (q.size()) {
ll u = q.front(); q.pop();
idx++;
for (auto p = e[u].begin(); p != e[u].end(); p++) {
ll v = *p; din[v]--;
if (din[v] == 0) {
q.push(v);
}
}
}
//cout << idx << " " << s.size() << endl;
if (idx != s.size())return true;
return false;
}
bool vis[N];
void bfs(ll s) {
for (int i = 1; i <= n; i++)vis[i] = 0;
queue<ll>q; q.push(s);
ll idx = -1;
while (q.size()) {
ll u = q.front(); q.pop();
if (vis[u])continue;
idx++;
vis[u] = 1;
for (auto p = e[u].begin(); p != e[u].end(); p++) {
ll v = *p;
if (vis[v] == 0)q.push(v);
}
}
//cout << s << " " << idx << endl;
if (idx > n / 2)f[s] = 0;
}
void solve()
{
//这个赛时就有思路,感觉是拓扑,正走一遍反走一遍,维护每个元素一定比他大的个数,一定比他小的个数
//但是,怎么说,,先,怎么没思路
//两遍拓扑即可,应该
//感觉就是一个dfs,回来写,不对,这样被办法准确找到一定大于的元素,看看题再说
//哦,不是,这纯水题啊,我还在想分叉怎么处理,结果根本不分叉,其实就直直一条线
//建好图,入度为0 的点入队就完了,水的不行,裸的拓扑吧
//我是罪人,m的边1到n^2不一定为连通图,所以判环应该要利用出现的节点的数量,不应该使用n
//唉,还是缺少经验,写的太少,细节不到位
//不光细节没处理好,裸拓扑不对,他给出的只是一种正确的可能,细节判断还得暴搜,拓扑判个环得了
cin >> n >> m;
for (int i = 1; i <= n; i++) {
f[i] = 1; din[i] = 0;
}
for (int i = 1; i <= m; i++) {
ll u, v; cin >> u >> v;
s.insert(u); s.insert(v);
op.push_back({ u,v });
}
//走一遍顺势
for (auto p = op.begin(); p != op.end(); p++) {
ll u = p->X, v = p->Y;
e[u].push_back(v);
din[v]++;
}
if (topo()) {
for (int i = 1; i <= n; i++)cout << 0;
cout << endl;
op.clear(); s.clear();
for (int i = 1; i <= n; i++) {
e[i].clear();
}
return;
}
//当前为正序
for (auto p = s.begin(); p != s.end(); p++) {
if (f[*p])bfs(*p);
}
for (auto p = s.begin(); p != s.end(); p++) {
e[*p].clear();
}
for (auto p = op.begin(); p != op.end(); p++) {
ll v = p->X, u = p->Y;
e[u].push_back(v);
}
for (auto p = s.begin(); p != s.end(); p++) {
if (f[*p] == 1)bfs(*p);
}
for (int i = 1; i <= n; i++)cout << f[i];
cout << endl;
op.clear(); s.clear();
for (int i = 1; i <= n; i++)e[i].clear();
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
//read(T);
while (T--)
solve();
return 0;
}
/*PonyHex*/
ll ksm(ll a, ll b) {
ll base = a;
ll ans = 1;
while (b) {
if (b & 1)ans *= base % mod;
ans %= mod;
base *= base; base %= mod;
b >>= 1;
}
return ans % mod;
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3888kb
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: 3832kb
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