内网传送门
线段树优化建图+拓扑排序。注意赋值从
1
1
1开始。
#include<bits/stdc++.h>
#define re register
#define cs const
cs int N=3e6+10,oo=1e9;
namespace IO{
cs int Rlen=1<<22|1;
char buf[Rlen],*p1,*p2;
inline char gc(){return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;}
template<typename T>
inline T get(){
char ch;T x;
while(!isdigit(ch=gc()));x=ch^48;
while(isdigit(ch=gc())) x=((x+(x<<2))<<1)+(ch^48);
return x;
}
inline int gi(){return get<int>();}
}
using namespace IO;
int n,s,m,a[N],b[N],in[N],tot;
int Head[N],Next[N],V[N],W[N],cnt=0;
inline void add(int u,int v,int w){Next[++cnt]=Head[u],++in[v],W[cnt]=w,V[cnt]=v,Head[u]=cnt;}
#define mid (T[root].l+T[root].r>>1)
#define lc (root<<1)
#define rc (root<<1|1)
struct node{int l,r,id;}T[N];
inline void build(int root,int l,int r){
T[root].l=l,T[root].r=r;
if(l==r){T[root].id=l;return;}
T[root].id=++tot;
build(lc,l,mid),build(rc,mid+1,r);
add(T[lc].id,T[root].id,0),add(T[rc].id,T[root].id,0);
}
inline void link(int root,int l,int r,int v){
if(l<=T[root].l&&T[root].r<=r){add(T[root].id,v,0);return;}
if(l> mid) return link(rc,l,r,v);
if(r<=mid) return link(lc,l,r,v);
return link(lc,l,r,v),link(rc,l,r,v);
}
int val=0,f[N];
inline bool topsort(){
std::queue<int> Q;
for(int re i=1;i<=n;++i){
f[i]=a[i]?a[i]:0;
if(!in[i]) f[i]=a[i]?a[i]:1,Q.push(i);
}
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int re i=Head[u],v=V[i];i;v=V[i=Next[i]]){
f[v]=std::max(f[v],f[u]+W[i]);
if(f[v]>oo) return 0;
if(a[v]&&f[v]>a[v]) return 0;
if(!(--in[v])) Q.push(v);
}
}
for(int i=1;i<=n;++i)
if(f[i]>oo || (a[i]&&f[i]>a[i]) || (!f[i])) return 0;
}
int main(){
// freopen("web.in","r",stdin);
// freopen("web.out","w",stdout);
n=gi(),s=gi(),m=gi(),tot=n;
for(int re i=1;i<=s;++i) a[gi()]=gi();
build(1,1,n);
for(int re i=1,l,r,k;i<=m;++i){
l=gi(),r=gi(),k=gi(),b[0]=l-1,++tot;
for(int re j=1;j<=k;++j){
b[j]=gi(),add(tot,b[j],1);
if(b[j-1]+1<=b[j]-1) link(1,b[j-1]+1,b[j]-1,tot);
}if(r>=b[k]+1) link(1,b[k]+1,r,tot);
}
if(!topsort()) return puts("Impossible"),0;
puts("Possible");
for(int i=1;i<=n;++i) printf("%d ",f[i]);
}