1. 程式人生 > >2016 World Final F Longest Rivers

2016 World Final F Longest Rivers

題意:

有n個河流@#¥……()……)——

思路:

考慮河流i,讓他儘量排在最前的話,肯定是他直接走到根,對於其他的葉子節點,如果他比河流i短的話,他可以向上覆蓋一定的邊。所以,還有一些邊是沒有被覆蓋的, 對這些邊做一顆生成樹,那麼比這個河流長的河流個數就是1 + sum {deg[i] - 1}。因此,可以按照dep從大到小來考慮每一個河流,同時,一些邊會在某些時刻加到生成樹裡面,維護1 +sum { deg[i] - 1 }就行了。

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define mod 1000000007
#define eps 1e-12
#define Pi acos(-1.0)
#define N 1000020
#define M 2000020
#define PB push_back
#define MP make_pair
#define fi first
#define se second

const LL inf = 1LL << 60;

int fst[N], nxt[M], vv[M], cost[M], e;

void init() {
	memset(fst, -1, sizeof fst);
	e = 0;
}
void add(int u, int v, int c) {
	vv[e] = v, cost[e] = c, nxt[e] = fst[u], fst[u] = e++;
}

int n, m, fa[N], bt[N];
LL dis[N], midis[N];
pair<LL, int> id[N];
vector<int> g[N];
bool vis[N];
string name[N];
char s[100];
int ans[N];
int tot;
int deg[N];

void dfs(int u, int p) {
	fa[u] = p;
	midis[u] = inf;
	for(int i = fst[u]; ~i; i = nxt[i]) {
		int v = vv[i];
		dis[v] = dis[u] + cost[i];
		bt[v] = cost[i];
		dfs(v, u);
		midis[u] = min(midis[u], cost[i] + midis[v]);
	}
	if(midis[u] == inf) midis[u] = 0;
}
void add_it(int u) {
	if(vis[u]) return;
	vis[u] = 1;
	int p = fa[u];
	deg[p]++;
	if(tot == 0) tot++;
	else
		if(deg[p] > 1) tot++;
	add_it(p);
}
int main() {
	init();
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i) {
		int p, c;
		scanf("%s%d%d", s, &p, &c);
		add(p + 1 + n, i, c);
		name[i] = s;
	}
	for(int i = 1; i <= m; ++i) {
		int p, c;
		scanf("%d%d", &p, &c);
		add(p + 1 + n, i + n + 1, c);
	}
	dfs(n + 1, 0);
	for(int i = 1; i <= n; ++i) id[i] = MP(dis[i], i);
	sort(id + 1, id + n + 1);

	for(int i = 1; i <= n + m + 1; ++i) {
		if(i == n + 1) continue;
		LL t = midis[i] + bt[i];
		int p = lower_bound(id + 1, id + n + 1, MP(t, -1)) - id;
		g[p].PB(i);
		if(p > n) {
			vector<int> gg;
			gg[1] = -1;
		}
	}
	tot = 0;
	vis[n + 1] = 1;
	for(int i = n; i >= 1; --i) {
		int u = id[i].se;
		ans[u] = tot;
		for(auto v: g[i]) {
			add_it(v);
		}
	}
	for(int i = 1; i <= n; ++i) printf("%s %d\n", name[i].c_str(), ans[i] + 1);
	return 0;
}


相關推薦

2016 World Final F Longest Rivers

題意: 有n個河流@#¥……()……)—— 思路: 考慮河流i,讓他儘量排在最前的話,肯定是他直接走到根,對於其他的葉子節點,如果他比河流i短的話,他可以向上覆蓋一定的邊。所以,還有一些邊是沒有被覆蓋的, 對這些邊做一顆生成樹,那麼比這個河流長的河流個數就是1 + sum

2016 World Final K String Theory

題意: 遞迴定義了k-quotation是k個分號+若干個(k-1)-quotataion+k個分號,然後給你很多個分號,叫你求一下最大是多少quotation。 思路: 對於一個k-quotation,分號的字首形式肯定是(k)(k-1)(k-2)..(1)(1),然後後

2016 World Final D Clock Breaking

題意: 給一個LCD燈在一個連續時間內的燈的情況,要你確定哪些燈一定亮,一定暗和不確定。 思路: 先預處理出每個燈泡在24*60分鐘內的狀態,然後用給的時間內不是全亮也不是全暗的燈取匹配,如果沒有一個時間點能夠匹配,就是invalid。然後在考慮全亮或者全暗的燈,如果有一

Gym 101194L / UVALive 7908 - World Cup - [三進制狀壓暴力枚舉][2016 EC-Final Problem L]

printf attach pre uvalive set 結果 題意 fin max 題目鏈接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.p

World Final 2015 F. Keyboarding

Keyboarding 這是一道BFS(但用到了不少的SPFA的思想)的題,不過出題者十分毒瘤,有眾多坑點: 1、按下方向鍵時跳到下一個不同的字元,也就意味著游標可以“傳送”。 2、最後要列印一個回車,也就是“*”號。 3、選擇也是一次操作。 4、可能有多組資料。 5、因為同樣的字母會重複出現,所

[World Final 2016] Branch Assignment

連結 PDF bzoj 先求出正置邊和反置邊時b+1到前b個點的最短路dis[0/1][x](x∈[1,b]), 令D[x]=dis[0][x]+dis[1][x] 然後分組後每個x對代價的貢獻為D[x]*(所在組中元素個數-1) 考慮DP決策分組過程,發現沒有一個很好的序, 不過為了使得代價小

[World Final 2017 F] Posterize (DP)

菜雞選手只能刷刷水題了,這個題O(n^3)的dp還是很顯然的. #include <bits/stdc++.h> #define ll long long using namespace

Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

div c++ cat have font pie sca allow mda F. Group Projects There are n students in a class working on group projects. The st

“Hello World!”Final發布文案加美工

分享 分享圖片 .com 面向 簡要介紹 功能 font 原型 普通 文案: 大家好,我們是“Hello World!”團隊,本次我將向大家簡要介紹一下空天獵的final發布,在空天獵final發布中,我主要從以下兩個方面向大家進行介紹,第一個方面是增加了敵方的boss功能

Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]

題目連結: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem

Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+貪心][2016 EC-Final Problem D]

題目連結: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem

ACM-ICPC 2018 world final A題 Catch the Plane

題目連線:https://icpc.kattis.com/problems/catch Catch the Plane Your plane to the ICPC Finals departs in a short time, and the only way to get

Uva 1040 狀壓+搜尋 2005 ACM world final problem c

題目的隱含條件將這道題指向了最小生成樹; 利用類似prim的方法,列舉所有子圖並判斷是否包含詢問點,如果包含那麼可以更新答案; 邊統計邊更新,且由於更新一定是向更多的點狀態下更新,所以一定可以統計到答案,不至於到全部是inf的情況 再更新答案時記錄ps,pe兩個變數分別表示此狀態最後一次更新前的狀態,邊

解題:BZOJ 2673 World Final 2011 Chips Challenge

題面 資料範圍看起來很像網路流誒(滾那 因為限制多而且強,資料範圍也不大,我們考慮不直接求答案,而是轉化為判定問題 可以發現第二個限制相對好滿足,我們直接列舉這個限制就可以。具體來說是列舉所有行中的最大值$x$,然後下面那個式子移項就可以得到$a*tot>=b*x$,其中tot表示晶片的總數 然

Problem H. Great Cells(2016 China-Final)【數學計數+智力題】

source:題目連結 題意:這是2016 ACM-ICPC China-Final的H題,在N×M的網格里填[1,K]的整數,定義一個格子是great的,如果滿足這個格子中的數是本行和本列中嚴格的最

2014ICPC World final A題(未解之謎)

未解之謎,先mark一下。 #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream

World Final 1999 poj 1873 The Fortified Forest 狀壓列舉 凸包

2^15 的複雜度  直接列舉集合   但是...   poj tle uvaliva上ac的程式碼: //#include<iostream> #include<cmath> #include<cstdio> #include<

2016 China Final H Great Cells 貢獻和思想

/** Ag其實是很難求的,需要從整體把握 sigma{(g + 1) * Ag} = sigma{Ag} + sigma{g * Ag},而 1.sigma{Ag}其實是所有情況 2.g * Ag = Ag + Ag + ... + Ag ,將

Problem D. Ice Cream Tower(2016 China-Final)【二分答案+貪心檢驗】

source:題目連結 題意:這是2016 ACM-ICPC China-Final的D題,一共有N個冰淇淋球,做一個冰淇淋需要K個球,並且由於穩定性,這K個球還必須滿足上下相鄰的下面比上面大至少兩倍

2017 World Final專題

C:二分匹配使得至多的行列要求相同最多在同一個位置 #include<iostream> #include<cstring> #include<iostream> #include<cstdio> #include<