[POI2015]PUS
嘟嘟嘟
这题只要往正确的方面想,就很简单。
首先,这是一道图论题!
想到这,这题就简单了。对于两个数\(i\)和\(j\),如果\(i\)比\(j\)大,就从\(i\)向\(j\)连边。然后如果图中存在环的话就无解,否则DAG上dp就完事啦。
但是如果暴力连边,最高就能达到\(O(k ^ 3)\)复杂度。然后考虑到是向连续区间连边,就可以线段树优化建图了。
我刚开始就这么写的,过是过了,但后来看题解才发现我这最坏也能达到\(O(n ^ 2logn)\)复杂度,实际上应该把这\(k\)个点向一个虚拟结点连边权为1的边,然后虚拟点向区间连边权为0的边,就能避免两两匹配\(O(n ^ 2)\)了。
就放一个我刚开始写的不太完美的代码吧,题解说的懒得写了。
19.7.8update,关于找环,直接拓扑排序,最后看还有没有入度为0的点就行了。(看以前的代码,还多写了个tarjan缩点,判联通块点数是否大于1……麻烦了)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 1e9;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
const int maxN = 4e6 + 5;
const int maxe = 5e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, s, val[maxn], pos[maxn], du[maxN];
struct Edge
{
int nxt, to, w;
}e[maxe];
int head[maxN], ecnt = -1;
In void addEdge(int x, int y, int w)
{
++du[y];
e[++ecnt] = (Edge){head[x], y, w};
head[x] = ecnt;
}
int l[maxn << 2], r[maxn << 2], tIn[maxn << 2], tcnt = 0;
In void build(int L, int R, int now)
{
l[now] = L; r[now] = R;
if(L == R) {tIn[now] = L; return;}
tIn[now] = ++tcnt;
int mid = (L + R) >> 1;
build(L, mid, now << 1);
build(mid + 1, R, now << 1 | 1);
addEdge(tIn[now], tIn[now << 1], 0);
addEdge(tIn[now], tIn[now << 1 | 1], 0);
}
In void update(int L, int R, int now, int x, int w)
{
if(L > R) return;
if(l[now] == L && r[now] == R)
{
addEdge(x, tIn[now], w);
return;
}
int mid = (l[now] + r[now]) >> 1;
if(R <= mid) update(L, R, now << 1, x, w);
else if(L > mid) update(L, R, now << 1 | 1, x, w);
else update(L, mid, now << 1, x, w), update(mid + 1, R, now << 1 | 1, x, w);
}
int dp[maxN];
In bool topo()
{
fill(dp + 1, dp + tcnt + 1, INF);
queue<int> q;
for(int i = 1; i <= tcnt; ++i) if(!du[i]) q.push(i);
while(!q.empty())
{
int now = q.front(); q.pop();
if(val[now])
{
if(dp[now] < val[now]) return 0;
dp[now] = val[now];
}
for(int i = head[now], v; ~i; i = e[i].nxt)
{
v = e[i].to;
dp[v] = min(dp[v], dp[now] - e[i].w);
if(!--du[v]) q.push(v);
}
}
for(int i = 1; i <= tcnt; ++i) if(du[i]) return 0;
return 1;
}
int main()
{
// freopen("ha.in", "r", stdin);
// freopen("ha.out", "w", stdout);
Mem(head, -1);
n = read(), s = read(), m = read();
for(int i = 1, x; i <= s; ++i) x = read(), val[x] = read();
tcnt = n; build(1, n, 1);
for(int i = 1; i <= m; ++i)
{
int L = read(), R = read(), K = read();
pos[0] = L - 1;
for(int j = 1; j <= K; ++j) pos[j] = read();
for(int j = 1; j <= K; ++j)
{
for(int k = 1; k <= K; ++k)
update(pos[k - 1] + 1, pos[k] - 1, 1, pos[j], 1);
update(pos[K] + 1, R, 1, pos[j], 1);
}
}
if(!topo()) {puts("NIE"); return 0;}
puts("TAK");
for(int i = 1; i <= n; ++i) write(val[i] ? val[i] : dp[i]), space; enter;
return 0;
}