Submission #41959796
Source Code Expand
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN = 2e5+10;
int n,m;
vector<int>e[MAXN];
int L[MAXN],R[MAXN];
int deg[MAXN],topo[MAXN],tot;
queue<int>q;
vector<int>occ[MAXN];
int ret[MAXN],pos[MAXN];
struct Node{
int u;
bool operator<(const Node& n2)const{
return R[u] > R[n2.u];
}
};
priority_queue<Node>pq;
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
rep(i,1,m){
int u,v;cin>>u>>v;
e[u].push_back(v);
deg[v]++;
}
rep(i,1,n)cin>>L[i]>>R[i];
rep(i,1,n)if(!deg[i])q.push(i),topo[++tot]=i;
while(q.size()){
int u=q.front();q.pop();
for(auto v:e[u]){
deg[v]--;
if(!deg[v]){
q.push(v),topo[++tot]=v;
}
}
}
if(tot < n){
cout<<"No\n";
return 0;
}
rep(i,1,n){
int u = topo[i];
for(auto v:e[u])L[v] = max(L[v],L[u]+1);
}
per(i,n,1){
int u = topo[i];
for(auto v:e[u])R[u] = min(R[u],R[v]-1);
}
rep(i,1,n){
if(L[i] > R[i]){
cout<<"No\n";
return 0;
}
occ[L[i]].push_back(i);
}
rep(i,1,n){
for(auto u:occ[i])pq.push((Node){u});
if(pq.empty()){
cout<<"No\n";
return 0;
}
Node nd = pq.top();pq.pop();
if(i > R[nd.u]){
cout<<"No\n";
return 0;
}
ret[i] = nd.u;
pos[nd.u] = i;
}
rep(i,1,n){
int u = topo[i];
for(auto v:e[u])if(pos[u] > pos[v]){
swap(ret[pos[u]],ret[pos[v]]);
swap(pos[u],pos[v]);
}
}
rep(i,1,n)assert(pos[i] >= L[i] && pos[i] <= R[i]);
cout<<"Yes\n";
rep(i,1,n)cout<<pos[i]<<" ";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | Ex - Constrained Topological Sort |
| User | Crying |
| Language | C++ (GCC 9.2.1) |
| Score | 625 |
| Code Size | 1899 Byte |
| Status | AC |
| Exec Time | 229 ms |
| Memory | 30084 KB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 625 / 625 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, 064.txt, 065.txt, 066.txt, 067.txt, 068.txt, 069.txt, 070.txt, 071.txt, 072.txt, 073.txt, 074.txt, 075.txt, 076.txt, 077.txt, 078.txt, 079.txt, 080.txt, 081.txt, 082.txt, example0.txt, example1.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 75 ms | 19196 KB |
| 001.txt | AC | 87 ms | 23024 KB |
| 002.txt | AC | 66 ms | 22640 KB |
| 003.txt | AC | 61 ms | 22068 KB |
| 004.txt | AC | 165 ms | 30084 KB |
| 005.txt | AC | 114 ms | 22280 KB |
| 006.txt | AC | 113 ms | 22260 KB |
| 007.txt | AC | 118 ms | 24508 KB |
| 008.txt | AC | 100 ms | 23564 KB |
| 009.txt | AC | 97 ms | 23712 KB |
| 010.txt | AC | 137 ms | 29264 KB |
| 011.txt | AC | 116 ms | 28712 KB |
| 012.txt | AC | 114 ms | 28740 KB |
| 013.txt | AC | 121 ms | 25028 KB |
| 014.txt | AC | 102 ms | 24376 KB |
| 015.txt | AC | 102 ms | 24980 KB |
| 016.txt | AC | 151 ms | 21400 KB |
| 017.txt | AC | 151 ms | 21188 KB |
| 018.txt | AC | 157 ms | 21480 KB |
| 019.txt | AC | 157 ms | 21384 KB |
| 020.txt | AC | 156 ms | 21784 KB |
| 021.txt | AC | 113 ms | 25696 KB |
| 022.txt | AC | 93 ms | 25356 KB |
| 023.txt | AC | 88 ms | 25044 KB |
| 024.txt | AC | 76 ms | 16232 KB |
| 025.txt | AC | 75 ms | 16252 KB |
| 026.txt | AC | 70 ms | 16108 KB |
| 027.txt | AC | 32 ms | 13824 KB |
| 028.txt | AC | 31 ms | 13824 KB |
| 029.txt | AC | 23 ms | 13828 KB |
| 030.txt | AC | 20 ms | 13404 KB |
| 031.txt | AC | 24 ms | 13312 KB |
| 032.txt | AC | 19 ms | 13352 KB |
| 033.txt | AC | 47 ms | 14344 KB |
| 034.txt | AC | 45 ms | 14232 KB |
| 035.txt | AC | 41 ms | 14248 KB |
| 036.txt | AC | 15 ms | 13228 KB |
| 037.txt | AC | 16 ms | 13232 KB |
| 038.txt | AC | 15 ms | 13152 KB |
| 039.txt | AC | 26 ms | 13276 KB |
| 040.txt | AC | 25 ms | 13476 KB |
| 041.txt | AC | 23 ms | 13464 KB |
| 042.txt | AC | 19 ms | 13212 KB |
| 043.txt | AC | 21 ms | 13236 KB |
| 044.txt | AC | 18 ms | 13280 KB |
| 045.txt | AC | 12 ms | 13052 KB |
| 046.txt | AC | 15 ms | 13048 KB |
| 047.txt | AC | 12 ms | 12984 KB |
| 048.txt | AC | 200 ms | 28908 KB |
| 049.txt | AC | 197 ms | 28900 KB |
| 050.txt | AC | 201 ms | 28904 KB |
| 051.txt | AC | 210 ms | 28880 KB |
| 052.txt | AC | 204 ms | 28916 KB |
| 053.txt | AC | 204 ms | 27336 KB |
| 054.txt | AC | 192 ms | 27292 KB |
| 055.txt | AC | 198 ms | 27364 KB |
| 056.txt | AC | 204 ms | 27332 KB |
| 057.txt | AC | 206 ms | 27284 KB |
| 058.txt | AC | 196 ms | 26764 KB |
| 059.txt | AC | 199 ms | 26792 KB |
| 060.txt | AC | 208 ms | 26688 KB |
| 061.txt | AC | 219 ms | 26760 KB |
| 062.txt | AC | 225 ms | 26712 KB |
| 063.txt | AC | 218 ms | 26660 KB |
| 064.txt | AC | 205 ms | 26568 KB |
| 065.txt | AC | 208 ms | 26564 KB |
| 066.txt | AC | 203 ms | 26628 KB |
| 067.txt | AC | 212 ms | 26560 KB |
| 068.txt | AC | 227 ms | 26552 KB |
| 069.txt | AC | 222 ms | 26520 KB |
| 070.txt | AC | 223 ms | 26464 KB |
| 071.txt | AC | 229 ms | 26560 KB |
| 072.txt | AC | 225 ms | 26464 KB |
| 073.txt | AC | 58 ms | 14868 KB |
| 074.txt | AC | 98 ms | 18232 KB |
| 075.txt | AC | 118 ms | 18716 KB |
| 076.txt | AC | 91 ms | 16536 KB |
| 077.txt | AC | 30 ms | 15120 KB |
| 078.txt | AC | 158 ms | 21236 KB |
| 079.txt | AC | 162 ms | 21236 KB |
| 080.txt | AC | 153 ms | 21236 KB |
| 081.txt | AC | 158 ms | 21248 KB |
| 082.txt | AC | 159 ms | 21248 KB |
| example0.txt | AC | 15 ms | 12904 KB |
| example1.txt | AC | 10 ms | 12932 KB |