【HDU5638 BestCoder Round 74 (div1)C】【贪心 线段树or树套树or队列】Toposort n点m边删k边使得拓扑序最小

Toposort

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 256    Accepted Submission(s): 99


Problem Description
There is a directed acyclic graph with   n  vertices and   m  edges. You are allowed to delete exact   k  edges in such way that the lexicographically minimal topological sort of the graph is minimum possible.
 

Input
There are multiple test cases. The first line of input contains an integer   T  indicating the number of test cases. For each test case:

The first line contains three integers   n ,   m  and   k   (1n100000,0km200000)  -- the number of vertices, the number of edges and the number of edges to delete.

For the next   m  lines, each line contains two integers   ui  and   vi , which means there is a directed edge from   ui  to   vi   (1ui,vin) .

You can assume the graph is always a dag. The sum of values of   n  in all test cases doesn't exceed   106 . The sum of values of   m  in all test cases doesn't exceed   2×106 .
 

Output
For each test case, output an integer   S=(i=1nipi) mod (109+7) , where   p1,p2,...,pn  is the lexicographically minimal topological sort of the graph.
 

Sample Input
  
  
3 4 2 0 1 2 1 3 4 5 1 2 1 3 1 4 1 2 3 2 4 4 4 2 1 2 2 3 3 4 1 4
 

Sample Output
  
  
30 27 30
 

Source


【HDU5638 BestCoder Round 74 (div1)C】【贪心 线段树二分】Toposort n点m边删k边使得拓扑序最小

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 2e5 + 10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m, k;
int x, y;
int ind[N];
vector<int>a[N];

//下标为入度,v为数值,d为入度(即下标)
int d[1 << 19];

void pushup(int o)
{
	d[o] = min(d[ls], d[rs]);
}

void build(int o, int l, int r)
{
	if (l == r)
	{
		d[o] = ind[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(lson);
	build(rson);
	pushup(o);
}

int V;
void change(int o, int l, int r)
{
	if (l == r)
	{
		d[o] = ind[l];
		return;
	}
	int mid = (l + r) >> 1;
	if (V <= mid)change(lson);
	else change(rson);
	pushup(o);
}
void check(int o, int l, int r)
{
	if (l == r)
	{
		V = l;
		d[o] = 1e9;
		return;
	}
	int mid = (l + r) >> 1;
	if (d[ls] <= k)check(lson);
	else check(rson);
	pushup(o);
}

int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 1; i <= n; ++i)
		{
			ind[i] = 0;
			a[i].clear();
		}
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d", &x, &y);
			++ind[y];
			a[x].push_back(y);
		}
		build(1, 1, n);
		int ans = 0;
		for (LL i = 1; i <= n; ++i)
		{
			check(1, 1, n); x = V;
			ans = (ans + i*x) % Z;
			k -= ind[x];
			ind[x] = 1e9;

			//删掉所有关联入度
			for (int j = a[x].size() - 1; ~j; --j)
			{
				int y = a[x][j]; if (ind[y]==1e9)continue;
				--ind[y];
				V = y; change(1, 1, n);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
/*
【吐槽&&trick】
1,由于我们一定可以查找到,而且查找的左界是1,所以我们不需要L,R做限定。
	可以直接check(lson)然后check(rson)

【题意】
给你一个图,图上有n(1e5)点,m(2e5)边。保证是个DAG
我们最多可以删掉k(0<=k=m)条边
使得字典序最小的拓扑序列尽可能小。

【类型】
贪心 构造 数据结构-树套树or线段树

【分析】
这题首先,我们有一个明显的贪心决策。
我们从前向后逐位枚举。
如果我们可以把第i位的数变成x,那它肯定就不会是x+1。

于是,我们做n次操作。
每次操作查找当前入度<=k的数中权值最小的点。

==========================树套树做法===================
这里有两个维度。
首先我们以入度为下标建立线段树(因为入度可能为0,所以要权值映射+1)。
然后维护最小权值的数。
然而,因为入度相同的点可能有多个。

所以对于每个入度,我们用set维护编号最小的点。
对于所有入度,用线段树维护区间段编号最小的点。

这样这道题就可以暴力做完啦。

==========================线段树做法====================
这道题还可以只用线段树就解决。
我们维护线段数,维护点值在某个区间段条件下的最小入度数。
这样在查询的时候,就可以在线段树上选择往左走还是往右走。
一样可以谈心AC

【时间复杂度&&优化】
O(nlogn)

*/

【HDU5638 BestCoder Round 74 (div1)C】【贪心 树套树】Toposort n点m边删k边使得拓扑序最小

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 2e5+10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m, k;
int x, y;
int ind[N];
vector<int>a[N];
set<int>sot[M];
int p[N];
bool e[N];
int cnt()
{
	int ret = 0;
	for (LL i = 1; i <= n; ++i)ret = (ret + p[i] * i) % Z;
	return ret;
}

//下标为入度,v为数值,d为入度(即下标)
int v[1 << 19];
int d[1 << 19];

void pushup(int o)
{
	if (v[ls] < v[rs])
	{
		v[o] = v[ls];
		d[o] = d[ls];
	}
	else
	{
		v[o] = v[rs];
		d[o] = d[rs];
	}
}

void build(int o, int l, int r)
{
	if (l == r)
	{
		v[o]=sot[l].empty()?1e9:*sot[l].begin();
		d[o] = l;
		return;
	}
	int mid=(l + r) >> 1;
	build(lson);
	build(rson);
	pushup(o);
}

int V, D, L, R;
void change(int o,int l,int r)
{
	if (l == r)
	{
		v[o] = V;
		return;
	}
	int mid = (l + r) >> 1;
	if (D <= mid)change(lson);
	else change(rson);
	pushup(o);
}

void check(int o, int l, int r)
{
	if (l >= L&&r <= R)
	{
		if (v[o] < V)
		{
			V = v[o];
			D = d[o];
		}
		return;
	}
	int mid=(l + r) >> 1;
	if (L <= mid)check(lson);
	if (R > mid)check(rson);
}

int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 1; i <= n; ++i)
		{
			ind[i] = 1;
			a[i].clear();
		}
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d", &x, &y);
			++ind[y];
			a[x].push_back(y);
		}
		if (k >= m)
		{
			for (int i = 1; i <= n; ++i)p[i] = i;
			printf("%d\n", cnt());
			continue;
		}
		for (int i = 1; i <= n; ++i)sot[i].clear();
		for (int i = 1; i <= n; ++i)sot[ind[i]].insert(i);
		build(1, 1, n);
		MS(e, 0);
		for (int i = 1; i <= n; ++i)
		{
			V = 1e9; L = 1; R = k + 1; 
			check(1, 1, n); 
			int x = V; p[i] = x;  e[x] = 1;	//找到具体的数值
			k -= (D-1);
			
			//删掉这个点
			sot[D].erase(x);
			V = sot[D].empty() ? 1e9 : *sot[D].begin();
			change(1, 1, n);

			//删掉所有关联入度
			for (int j = a[x].size() - 1; ~j; --j)
			{
				int y = a[x][j]; if (e[y])continue;

				//从原有入度中删除
				D = ind[y]--;
				if (y == *sot[D].begin())
				{
					sot[D].erase(y);
					V = sot[D].empty() ? 1e9 : *sot[D].begin();
					change(1, 1, n);
				}
				else sot[D].erase(y);

				//从新的入度中添加
				sot[--D].insert(y);
				if (y == *sot[D].begin())
				{
					V = y;
					change(1, 1, n);
				}
			}
		}
		printf("%d\n", cnt());
	}
	return 0;
}
/*
【题意】
给你一个图,图上有n(1e5)点,m(2e5)边。保证是个DAG
我们最多可以删掉k(0<=k=m)条边
使得字典序最小的拓扑序列尽可能小。

【类型】
贪心 构造 数据结构-树套树or线段树

【分析】
这题首先,我们有一个明显的贪心决策。
我们从前向后逐位枚举。
如果我们可以把第i位的数变成x,那它肯定就不会是x+1。

于是,我们做n次操作。
每次操作查找当前入度<=k的数中权值最小的点。

这里有两个维度。
首先我们以入度为下标建立线段树(因为入度可能为0,所以要权值映射+1)。
然后维护最小权值的数。
然而,因为入度相同的点可能有多个。

所以对于每个入度,我们用set维护编号最小的点。
对于所有入度,用线段树维护区间段编号最小的点。

这样这道题就可以暴力做完啦。

【时间复杂度&&优化】
O(nlogn)

*/
【HDU5638 BestCoder Round 74 (div1)C】【贪心 队列】Toposort n点m边删k边使得拓扑序最小

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 2e5 + 10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m, k;
int x, y;
int ind[N];
vector<int>a[N];
set<int>sot;
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 1; i <= n; ++i)
		{
			ind[i] = 0;
			a[i].clear();
		}
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d", &x, &y);
			++ind[y];
			a[x].push_back(y);
		}
		for (int i = 1; i <= n; ++i)if (ind[i] <= k)sot.insert(i);
		int ans = 0;
		LL id = 0; while (!sot.empty())
		{
			int x = *sot.begin();
			sot.erase(sot.begin());
			if (ind[x] > k)continue;
			k -= ind[x]; ind[x] = 1e9;
			ans = (ans + (++id)*x) % Z;
			for (int i = a[x].size() - 1; ~i; --i)
			{
				int y = a[x][i];
				if (--ind[y] <= k)sot.insert(y);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
/*
【吐槽&&trick】
1,由于我们一定可以查找到,而且查找的左界是1,所以我们不需要L,R做限定。
可以直接check(lson)然后check(rson)

【题意】
给你一个图,图上有n(1e5)点,m(2e5)边。保证是个DAG
我们最多可以删掉k(0<=k=m)条边
使得字典序最小的拓扑序列尽可能小。

【类型】
贪心 构造 数据结构-树套树or线段树

【分析】
这题首先,我们有一个明显的贪心决策。
我们从前向后逐位枚举。
如果我们可以把第i位的数变成x,那它肯定就不会是x+1。

于是,我们做n次操作。
每次操作查找当前入度<=k的数中权值最小的点。

==========================树套树做法===================
这里有两个维度。
首先我们以入度为下标建立线段树(因为入度可能为0,所以要权值映射+1)。
然后维护最小权值的数。
然而,因为入度相同的点可能有多个。

所以对于每个入度,我们用set维护编号最小的点。
对于所有入度,用线段树维护区间段编号最小的点。

这样这道题就可以暴力做完啦。

==========================线段树做法====================
这道题还可以只用线段树就解决。
我们维护线段数,维护点值在某个区间段条件下的最小入度数。
这样在查询的时候,就可以在线段树上选择往左走还是往右走。
一样可以谈心AC

==========================队列做法======================
这题还可以直接用队列搞。
我们直接用队列维护所有入度<=k的点,
然后每次取出<=k的点中数值最小的那个。
每个点只会在前导边被删掉的时候入队
时间复杂度O(mlog(n))

【时间复杂度&&优化】
O(nlogn)

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值