QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#485367 | #8593. Coin | shenfe1 | 3 | 2ms | 9952kb | C++23 | 2.2kb | 2024-07-20 17:10:05 | 2024-07-20 17:10:06 |
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define mem(a,i) memset(a,i,sizeof(a))
#define in insert
#define sz(s) (int)s.size()
const int MAX=5e5+10;
int n,m;
vector<int> g[MAX],g1[MAX];
int use[MAX];
int ind[MAX],outd[MAX];
int ans[MAX];
bool was1[MAX],was2[MAX];
struct DSU{
int f[MAX];
void init(int n){
for(int i=1;i<=n;i++)f[i]=i;
}
int get(int v){
return (f[v]==v?v:f[v]=get(f[v]));
}
void unite(int u,int v){
u=get(u);
v=get(v);
f[u]=v;
}
}D;
void solve(){
cin>>n>>m;
D.init(n);
int cnt=1;
multiset<int> st;
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g1[b].pb(a);
if(D.get(a)!=D.get(b)){
D.unite(a,b);
cnt++;
}
ind[b]++;
outd[a]++;
}
if(cnt<n){
for(int i=1;i<=n;i++)cout<<-1<<" ";
return;
}
for(int i=1;i<=n;i++){
if(ind[i]==0)st.in(i);
}
while(!st.empty()){
if(sz(st)==1){
// cout<<*st.begin()<<"\n";
ans[*st.begin()]++;
}
multiset<int> nw;
for(auto v:st){
for(auto to:g[v]){
ind[to]--;
if(ind[to]==0)nw.in(to);
}
}
swap(nw,st);
}
st.clear();
for(int i=1;i<=n;i++){
if(outd[i]==0)st.in(i);
}
while(!st.empty()){
if(sz(st)==1){
// cout<<*st.begin()<<"\n";
ans[*st.begin()]++;
}
multiset<int> nw;
for(auto v:st){
for(auto to:g1[v]){
outd[to]--;
if(outd[to]==0)nw.in(to);
}
}
swap(nw,st);
}
st.clear();
for(int i=1;i<=n;i++){
if(ans[i]>1)cout<<1<<" ";
else cout<<-1<<" ";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
詳細信息
Subtask #1:
score: 3
Acceptable Answer
Test #1:
score: 3
Acceptable Answer
time: 0ms
memory: 9952kb
input:
4 4 2 4 3 1 4 1 2 3
output:
1 1 -1 -1
result:
points 0.50 -1 correct
Test #2:
score: 3
Acceptable Answer
time: 2ms
memory: 9820kb
input:
6 8 1 5 5 4 6 2 2 5 4 3 6 1 6 5 2 1
output:
1 1 1 1 1 1
result:
points 0.50 -1 correct
Test #3:
score: 6
Accepted
time: 0ms
memory: 9732kb
input:
2 1 1 2
output:
1 1
result:
ok ac
Test #4:
score: 3
Acceptable Answer
time: 0ms
memory: 9784kb
input:
6 12 1 5 5 4 6 2 2 5 4 3 6 5 1 5 1 5 2 4 6 3 1 3 4 3
output:
-1 -1 1 1 1 -1
result:
points 0.50 -1 correct
Test #5:
score: 3
Acceptable Answer
time: 2ms
memory: 9724kb
input:
7 20 1 6 6 3 1 4 1 5 1 7 1 2 1 5 2 3 4 5 7 2 2 4 5 3 6 3 1 3 4 3 7 5 2 6 4 6 7 2 7 5
output:
1 1 1 1 -1 -1 1
result:
points 0.50 -1 correct
Test #6:
score: 3
Acceptable Answer
time: 0ms
memory: 9952kb
input:
7 20 5 6 1 3 3 6 4 1 7 4 2 5 4 3 2 6 7 5 4 6 2 6 2 1 4 5 1 3 1 5 7 1 7 6 4 1 7 6 3 6
output:
1 -1 -1 -1 -1 1 -1
result:
points 0.50 -1 correct
Test #7:
score: 3
Acceptable Answer
time: 2ms
memory: 9752kb
input:
7 20 7 6 4 5 6 4 3 6 4 1 6 2 3 5 5 2 7 6 1 2 3 6 6 4 7 1 6 1 7 1 4 5 3 6 3 5 4 5 3 1
output:
-1 1 -1 1 -1 1 -1
result:
points 0.50 -1 correct
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
50%
Acceptable Answer
Test #8:
score: 0
Wrong Answer
time: 0ms
memory: 9760kb
input:
20 100 5 20 4 5 18 16 1 13 14 9 11 19 6 4 7 20 16 11 8 13 4 5 16 9 12 14 7 12 11 3 9 11 9 11 13 6 3 10 12 9 13 4 20 12 13 6 18 11 5 7 5 7 15 18 12 15 17 13 15 18 3 2 11 2 11 2 15 19 4 19 14 19 14 9 17 3 1 18 8 10 16 19 1 6 7 2 5 12 1 18 8 20 5 18 8 5 4 16 1 15 5 19 18 19 17 10 1 10 17 3 10 2 3 10 17...
output:
-1 -1 -1 1 1 1 1 -1 1 1 1 1 1 -1 1 -1 -1 -1 -1 1
result:
wrong answer wa
Subtask #3:
score: 0
Skipped
Dependency #1:
50%
Acceptable Answer
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
50%
Acceptable Answer
Dependency #2:
0%