답안 #223752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223752 2020-04-16T09:53:40 Z arnold518 Election Campaign (JOI15_election_campaign) C++14
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N, Q;
vector<int> adj[MAXN+10];

int par[MAXN+10][25];
int dep[MAXN+10];
int L[MAXN+10], R[MAXN+10], cnt;

struct Query
{
	int u, v, w;
};
vector<Query> A[MAXN+10];

void dfs(int now, int bef, int d)
{
	dep[now]=d;
	par[now][0]=bef;
	L[now]=++cnt;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
	}
	R[now]=cnt;
}

int lca(int x, int y)
{
	int i, j;
	if(dep[x]<dep[y]) swap(x, y);
	for(i=20; i>=0; i--) if(dep[par[x][i]]>=dep[y]) x=par[x][i];
	if(x==y) return x;
	for(i=20; i>=0; i--) if(par[x][i]!=par[y][i]) x=par[x][i], y=par[y][i];
	return par[x][0];
}

struct BIT
{
	int tree[MAXN+10];
	void update(int i, int v) { for(; i<=N; i+=(i&-i)) tree[i]+=v; }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
	void update(int l, int r, int v) { update(l, v); update(r+1, -v); }
}
int dp[MAXN+10];

void dfs2(int now, int bef)
{
	int i, j;	
	vector<int> chd;

	int sum=0;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs2(nxt, now);
		chd.push_back(nxt);
		sum+=dp[nxt];
	}

	dp[now]=sum;

	for(auto nxt : chd) seg.update(1, 1, N, L[nxt], R[nxt], -dp[nxt]);	
	for(auto &it : A[now])
	{
		int v=seg.query(1, 1, N, L[it.u])+seg.query(1, 1, N, L[it.v])+sum+it.w;
		dp[now]=max(dp[now], v);
	}
	seg.update(1, 1, N, L[now], R[now], sum);
}

int main()
{
	int i, j;

	scanf("%d", &N);
	for(i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	dfs(1, 1, 1);
	for(i=1; i<=20; i++) for(j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];

	scanf("%d", &Q);
	while(Q--)
	{
		int u, v, w, k;
		scanf("%d%d%d", &u, &v, &k);
		w=lca(u, v);
		A[w].push_back({u, v, k});
	}

	dfs2(1, 1);
	printf("%d\n", dp[1]);
}

Compilation message

election_campaign.cpp: In function 'int lca(int, int)':
election_campaign.cpp:38:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
election_campaign.cpp: At global scope:
election_campaign.cpp:52:2: error: expected ';' after struct definition
 }
  ^
  ;
election_campaign.cpp: In function 'void dfs2(int, int)':
election_campaign.cpp:71:22: error: 'seg' was not declared in this scope
  for(auto nxt : chd) seg.update(1, 1, N, L[nxt], R[nxt], -dp[nxt]); 
                      ^~~
election_campaign.cpp:74:9: error: 'seg' was not declared in this scope
   int v=seg.query(1, 1, N, L[it.u])+seg.query(1, 1, N, L[it.v])+sum+it.w;
         ^~~
election_campaign.cpp:77:2: error: 'seg' was not declared in this scope
  seg.update(1, 1, N, L[now], R[now], sum);
  ^~~
election_campaign.cpp:57:6: warning: unused variable 'i' [-Wunused-variable]
  int i, j; 
      ^
election_campaign.cpp:57:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j; 
         ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &k);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~