답안 #475754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
475754 2021-09-24T01:46:30 Z CaroLinda Janjetina (COCI21_janjetina) C++14
110 / 110
1482 ms 38408 KB
#include <bits/stdc++.h>

#define debug printf
#define ff first
#define ss second
#define mkt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define pii pair<int,int>
#define mk make_pair
#define pb push_back
#define tiiii tuple<int,int,int, int>

const int MAXN = 1e5+10 ;

using namespace std ;

ll ans ;

struct Query
{

	vector<int> procuro ;
	int other_vert , edge_weight ;

	bool operator < ( Query other ) const { return edge_weight < other.edge_weight ; }

} ;

int N, K ;
int dsu[MAXN] , qtd[MAXN] , weight[MAXN] ;
vector<int> adj_dsu[MAXN] ;
vector<pair<int,int> > adj[MAXN] ;
vector< tuple<int,int,int, int> > edges ;
vector<Query> queries[MAXN] ;
bool marc[MAXN] ;

// ----------------------- UNION FIND -------------------------
int find(int x) { return dsu[x] = (x == dsu[x]) ? x : find(dsu[x]) ; }
int w_union_find ;
void dfs_union_find(int x, int y , int par, int depth )
{
	if(w_union_find-K-depth-1 >= 0)
		queries[y].back().procuro.pb( min(w_union_find-K-depth-1, N) ) ;

	for(auto e : adj_dsu[x] )
	{
		if(e == par ) continue ;
		dfs_union_find(e, y, x, depth+1) ;
	}
}
void join(int x, int y, int w, int i)
{
	int a = find(x) ;
	int b = find(y) ;
	w_union_find = w ;

	if( qtd[a] > qtd[b] )
	{
		swap(a,b) ;
		swap(x,y) ;
	}

	queries[y].pb( Query() ) ;
	queries[y].back().edge_weight = i-1 ;
	queries[y].back().other_vert = x ;

	dfs_union_find(x, y, -1, 0 ); 

	/*debug("Query guardada no %d com other_Vert = %d e edge_weight = %d\n",y , x, i-1 ) ;
	debug("Suas perguntas: ") ;
	for(auto e : queries[y].back().procuro ) debug("%d " , e ) ;
	debug("\n\n") ; */

	adj_dsu[x].pb(y) ;
	adj_dsu[y].pb(x) ;
	dsu[a] = b ;
	qtd[b] += qtd[a] ;

}
// ------------------------------------------------------------

int q ;
int sub[MAXN] , bit[MAXN] , dist[MAXN] ;
vector<pair<int,int> > pares , perguntas ;

void upd(int i , int val) { if(i == -1 ) return ;for(++i; i < MAXN ; i += i &-i ) bit[i] += val; }
int qry(int i)
{
	int tot = 0 ;
	for(++i; i > 0 ; i -= i &-i ) tot += bit[i] ;
	return tot ;
}

void dfs1(int x, int par )
{
	sub[x] = 1 ;
	for(auto e : adj[x])
		if(e.ff != par && !marc[e.ff])
		{
			dfs1(e.ff,x) ;
			sub[x] += sub[e.ff] ;
		}
}
int dfs2(int x, int par )
{
	for(auto e : adj[x] ) 
	{
		if(e.ff == par || marc[e.ff]) continue ;
		if( sub[e.ff] > q/2 ) return dfs2(e.ff, x ) ;
	}
	return x ;
}

void dfs3(int x, int par , int cur_mx , int cur_depth )
{

	dist[x] = cur_depth ;
	pares.pb( mk(cur_mx, cur_depth) ) ;

	for(int i = 0 ; i < sz(queries[x]) ; i++ )
	{
		if( queries[x][i].other_vert == par || cur_mx > queries[x][i].edge_weight ) continue ;
		perguntas.pb(mk(x,i)) ;
	}

	for(auto e : adj[x] ) 
	{
		if(marc[e.ff] || e.ff == par ) continue ;
		dfs3(e.ff, x, max(cur_mx, weight[e.ss]) , cur_depth+1 ) ;
	}

}

void solve(int ptr1, int ptr2, int s)
{
	while(ptr1 < sz(pares) || ptr2 < sz(perguntas))
	{
		if( ptr2 == sz(perguntas) ) 
		{
			upd(pares[ptr1].ss, 1) ;
			ptr1++ ;
			continue ;
		}

		int x = perguntas[ptr2].ff ;
		int y = perguntas[ptr2].ss ;
		int k = queries[ x ][y].edge_weight ;

		if(ptr1 == sz(pares) || pares[ptr1].ff > k)
		{
			//cout << x << " " << y << " " << k << " " << ptr1 << " " << ptr2 << " " << sz(perguntas) << endl ;
			for( auto e : queries[x][y].procuro )
				ans += qry( e - dist[x]) * s ;

			ptr2++ ;
			continue ;
		}
		else
		{
			upd(pares[ptr1].ss , 1 ) ;
			ptr1++ ;
			continue ;
		}

	}
}

bool cmp(pii p1, pii p2) { return queries[p1.ff][p1.ss] < queries[p2.ff][p2.ss] ; } 

void decompose(int x)
{

	dfs1(x,-1) ;
	q = sub[x] ;
	
	int cn = dfs2(x,-1) ;

	marc[cn] = true ;

	pares.clear() ; perguntas.clear() ;
	pares.pb( mk( -1, 0 ) ) ;

	for(auto e : adj[cn])
	{
		if(marc[e.ff]) continue ;

		int l_pares=  sz(pares) ;
		int l_perguntas = sz(perguntas) ;

		dfs3(e.ff, cn, weight[e.ss] , 1 ) ;

		sort( pares.begin()+l_pares , pares.end() ) ;
		sort( perguntas.begin()+l_perguntas, perguntas.end() , cmp ) ;

		int ptr1 = l_pares , ptr2 = l_perguntas ;

		/*debug("Na sub %d, adicionei %d pares e %d perguntas\n", e.ff, sz(pares)-ptr1 , sz(perguntas)-ptr2 ) ;
		for(int i = ptr1 ; i<sz(pares) ; i++ ) debug("(%d,%d) " , pares[i].ff, pares[i].ss );
		debug("\n") ;
		for(int i = ptr2 ; i < sz(perguntas) ; i++ ) debug("(%d %d) " , perguntas[i].ff, perguntas[i].ss ) ;
		debug("\n") ; */

		solve(ptr1, ptr2,-1) ;
		//debug("Nova ans %lld\n\n" , ans ) ;

		for(int i = l_pares ; i < sz(pares) ; i++ ) 
			upd( pares[i].ss , -1 ) ;

	}

	sort(all(pares)) ;
	sort(all(perguntas),cmp) ;

	solve(0,0,1) ;

	for(int i = 0 ; i < sz(pares) ; i++ ) 
		upd( pares[i].ss , -1 ) ;	

	perguntas.clear() ;
	for(int i = 0 ; i < sz(queries[cn]) ; i++ ) 
	{
		perguntas.pb(mk(cn,i)) ;
		queries[cn][i].edge_weight-- ;
	}

/*	for(int i = 0 ; i<sz(pares) ; i++ ) debug("(%d,%d) " , pares[i].ff, pares[i].ss );
	debug("\n") ;
	for(int i = 0 ; i < sz(perguntas) ; i++ ) debug("(%d %d) " , perguntas[i].ff, perguntas[i].ss ) ;
	debug("\n") ;	*/

	dist[cn] = 0 ;

	solve(0,0,1)  ;

	for(int i = 0 ; i < sz(pares) ; i++ ) 
		upd( pares[i].ss , -1 ) ;

	//debug("Nova ans %lld\n" , ans ) ;

	for(auto e : adj[cn])
		if(!marc[e.ff]) decompose(e.ff) ;	

}

int main()
{
	scanf("%d %d", &N, &K ) ;

	for(int i = 1 ; i <= N ; i++ )
	{
		dsu[i] = i ;
		qtd[i] = 1 ;
	}

	for(int i = 0, x , y , w ; i < N-1 ; i++ )
	{
		scanf("%d %d %d", &x, &y, &w ) ;
		adj[x].pb(mk(y,i)) ;
		adj[y].pb(mk(x,i) );
		edges.pb(mkt(w, x, y,i)) ;
	}

	sort(all(edges)) ;
	for(int i = 0 , x , y , w ; i < N-1; i++ )
	{
		w = get<0>(edges[i]) ;
		x = get<1>(edges[i]) ;
		y = get<2>(edges[i]) ;

		join(x,y,w, (i+1)*2 ) ;

		weight[get<3>(edges[i])] = (i+1)*2 ;
	}

	decompose(1) ;

	printf("%lld\n" , ans*2LL ) ;

}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:250:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  250 |  scanf("%d %d", &N, &K ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:260:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  260 |   scanf("%d %d %d", &x, &y, &w ) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7356 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 8 ms 7624 KB Output is correct
5 Correct 8 ms 7628 KB Output is correct
6 Correct 8 ms 7628 KB Output is correct
7 Correct 7 ms 7628 KB Output is correct
8 Correct 8 ms 7688 KB Output is correct
9 Correct 6 ms 7500 KB Output is correct
10 Correct 6 ms 7628 KB Output is correct
11 Correct 6 ms 7500 KB Output is correct
12 Correct 7 ms 7500 KB Output is correct
13 Correct 9 ms 7620 KB Output is correct
14 Correct 8 ms 7500 KB Output is correct
15 Correct 7 ms 7492 KB Output is correct
16 Correct 8 ms 7628 KB Output is correct
17 Correct 7 ms 7500 KB Output is correct
18 Correct 7 ms 7540 KB Output is correct
19 Correct 8 ms 7604 KB Output is correct
20 Correct 7 ms 7488 KB Output is correct
21 Correct 8 ms 7500 KB Output is correct
22 Correct 7 ms 7500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 6 ms 7372 KB Output is correct
4 Correct 9 ms 7756 KB Output is correct
5 Correct 50 ms 9928 KB Output is correct
6 Correct 332 ms 22692 KB Output is correct
7 Correct 546 ms 33180 KB Output is correct
8 Correct 932 ms 36736 KB Output is correct
9 Correct 541 ms 32992 KB Output is correct
10 Correct 607 ms 37424 KB Output is correct
11 Correct 551 ms 33264 KB Output is correct
12 Correct 871 ms 36808 KB Output is correct
13 Correct 522 ms 32944 KB Output is correct
14 Correct 677 ms 37436 KB Output is correct
15 Correct 601 ms 36060 KB Output is correct
16 Correct 692 ms 38112 KB Output is correct
17 Correct 702 ms 38408 KB Output is correct
18 Correct 700 ms 38332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7356 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 8 ms 7624 KB Output is correct
5 Correct 8 ms 7628 KB Output is correct
6 Correct 8 ms 7628 KB Output is correct
7 Correct 7 ms 7628 KB Output is correct
8 Correct 8 ms 7688 KB Output is correct
9 Correct 6 ms 7500 KB Output is correct
10 Correct 6 ms 7628 KB Output is correct
11 Correct 6 ms 7500 KB Output is correct
12 Correct 7 ms 7500 KB Output is correct
13 Correct 9 ms 7620 KB Output is correct
14 Correct 8 ms 7500 KB Output is correct
15 Correct 7 ms 7492 KB Output is correct
16 Correct 8 ms 7628 KB Output is correct
17 Correct 7 ms 7500 KB Output is correct
18 Correct 7 ms 7540 KB Output is correct
19 Correct 8 ms 7604 KB Output is correct
20 Correct 7 ms 7488 KB Output is correct
21 Correct 8 ms 7500 KB Output is correct
22 Correct 7 ms 7500 KB Output is correct
23 Correct 4 ms 7372 KB Output is correct
24 Correct 4 ms 7372 KB Output is correct
25 Correct 6 ms 7372 KB Output is correct
26 Correct 9 ms 7756 KB Output is correct
27 Correct 50 ms 9928 KB Output is correct
28 Correct 332 ms 22692 KB Output is correct
29 Correct 546 ms 33180 KB Output is correct
30 Correct 932 ms 36736 KB Output is correct
31 Correct 541 ms 32992 KB Output is correct
32 Correct 607 ms 37424 KB Output is correct
33 Correct 551 ms 33264 KB Output is correct
34 Correct 871 ms 36808 KB Output is correct
35 Correct 522 ms 32944 KB Output is correct
36 Correct 677 ms 37436 KB Output is correct
37 Correct 601 ms 36060 KB Output is correct
38 Correct 692 ms 38112 KB Output is correct
39 Correct 702 ms 38408 KB Output is correct
40 Correct 700 ms 38332 KB Output is correct
41 Correct 4 ms 7360 KB Output is correct
42 Correct 530 ms 33064 KB Output is correct
43 Correct 916 ms 36908 KB Output is correct
44 Correct 534 ms 33008 KB Output is correct
45 Correct 653 ms 37564 KB Output is correct
46 Correct 521 ms 33036 KB Output is correct
47 Correct 861 ms 36668 KB Output is correct
48 Correct 542 ms 33012 KB Output is correct
49 Correct 588 ms 37424 KB Output is correct
50 Correct 621 ms 35956 KB Output is correct
51 Correct 751 ms 37960 KB Output is correct
52 Correct 336 ms 26544 KB Output is correct
53 Correct 362 ms 30260 KB Output is correct
54 Correct 313 ms 27412 KB Output is correct
55 Correct 374 ms 30220 KB Output is correct
56 Correct 432 ms 29360 KB Output is correct
57 Correct 566 ms 25456 KB Output is correct
58 Correct 658 ms 30080 KB Output is correct
59 Correct 1482 ms 29700 KB Output is correct
60 Correct 700 ms 29168 KB Output is correct
61 Correct 715 ms 29364 KB Output is correct
62 Correct 502 ms 25648 KB Output is correct
63 Correct 612 ms 29876 KB Output is correct
64 Correct 644 ms 28700 KB Output is correct
65 Correct 23 ms 8396 KB Output is correct
66 Correct 5 ms 7372 KB Output is correct