写了一道许久之前写过的题,发现好久没写线段树了和图之类的东西,现在都很生疏了,一定要加强锻炼,增强代码能力,加油。
HDU5195
#include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define N 100000 + 10 #define INF 0x3f3f3f3f #define lson L, mid, rt << 1 #define rson mid + 1, R, rt << 1 | 1 int n, m, k, tot, head[N], deg[N], Min[N << 2]; vector<int> ans; struct Edge { int v, next; }e[N]; void init() { tot = 0; memset(deg, 0, sizeof deg); memset(head, -1, sizeof head); } void adde(int u, int v) { deg[v]++; e[tot].v = v; e[tot].next = head[u]; head[u] = tot++; } void pushup(int rt) { Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]); } void build(int L, int R, int rt) { if(L == R) { Min[rt] = deg[L]; return ; } int mid = (L + R) >> 1; build(lson); build(rson); pushup(rt); } void update(int q, int v, int L, int R, int rt) { if(L == R) { Min[rt] = v; return ; } int mid = (L + R) >> 1; if(q <= mid) update(q, v, lson); else update(q, v, rson); pushup(rt); } int query(int k, int L, int R, int rt) { if(L == R) { return L; } int mid = (L + R) >> 1; if(Min[rt << 1 | 1] <= k) return query(k, rson); else return query(k, lson); } int main() { while(~scanf("%d%d%d", &n, &m, &k)) { init(); int u, v; ans.clear(); for(int i = 0; i < m; i++) { scanf("%d%d", &u, &v); adde(u, v); } build(1, n, 1); for(int i = 1; i <= n; i++) { int id = query(k, 1, n, 1); k -= deg[id]; ans.push_back(id); deg[id] = INF; update(id, INF, 1, n, 1); for(int j = head[id]; j != -1; j = e[j].next) { int v = e[j].v; deg[v]--; update(v, deg[v], 1, n, 1); } } for(int i = 0; i < ans.size(); i++) i != ans.size() - 1 ? printf("%d ", ans[i]) : printf("%d\n", ans[i]); } return 0; }