QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#102757 | #5160. Kebab Pizza | w4p3r# | RE | 2ms | 23800kb | C++20 | 2.1kb | 2023-05-03 17:14:38 | 2023-05-03 17:14:42 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e - 6
#define FOR(i, a, b) for(int i = a;i <= b;i ++)
#define REP(i, a, b) for(int i = a;i >= b;i --)
#define pa pair<int, int>
#define fr first
#define sd second
#define pb push_back
#define db double
#define MEM(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
char ch = getchar();
ll s = 0, w = 1;
while (ch < '0' || ch > '9') {if (ch == '-')w = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
#define N 500010
int n, m;
int a[N], b[N];
vector<int>e[N];
int vst[N], st[N], top;
int d[N], deg[N], dep[N];
int flag = 0;
int id[N];
inline int ck(vector<int> G)
{
int tot = 0; int k = G.size();
for (int x : G)id[x] = ++ tot;
FOR(i, 1, m)
{
int x = a[i], y = b[i];
if ((!id[x]) && (!id[y])) {cout << "impossible\n"; exit(0);}
if (id[x] && id[y])
{
if (id[x] > id[y]) swap(x, y);
if ((id[y] != id[x] + 1) && (id[y] != k || id[x] != 1) && (x != y)) {cout << "impossible\n"; exit(0);}
}
}
cout << "possible\n" << '\n';
}
void dfs(int x, int fa)
{
// cerr << "EE:" << x << '\n';
dep[x] = dep[fa] + 1;
st[++ top] = x; vst[x] = 1;
for (int y : e[x]) if (y != fa)
{
if (vst[y])
{
if(dep[y] > dep[x]) continue;
// cerr << "??:" << x << " " << y << endl;
// FOR(i, 1, top) cerr << st[i] << ' '; cerr << '\n';
vector<int> G;
while (st[top] != y)
{
G.pb(st[top--]);
}
G.pb(y);
ck(G); exit(0);
}
dfs(y, x);
}
top --;
}
int main()
{
m = read(), n = read();
FOR(i, 1, m)
{
a[i] = read(), b[i] = read();
if (a[i] != b[i])e[a[i]].pb(b[i]), e[b[i]].pb(a[i]);
}
FOR(i, 1, n)if (!vst[i]) {dfs(i, 0);}
FOR(i, 1, m)
{
if(a[i] != b[i]) deg[a[i]] ++, deg[b[i]] ++;
}
FOR(i, 1, n)d[i] = deg[i];
FOR(i, 1, n)if(deg[i] == 1)
{
for(int j : e[i]) d[j] --;
}
FOR(i, 1, n)if(d[i] > 2){cout << "impossible\n"; exit(0);}
cout << "possible\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 23800kb
input:
7 6 2 2 3 6 1 1 1 5 4 5 6 6 6 5
output:
possible
result:
ok single line: 'possible'
Test #2:
score: -100
Dangerous Syscalls
input:
5 5 1 3 1 5 2 3 2 5 3 4