이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 200005;
int n,k;
vector<int> adj[MAXN];
int C[MAXN];
vector<int> V[MAXN];
using namespace std;
int twok[MAXN][20];
int depth[MAXN];
int st[MAXN], en[MAXN], back[MAXN];
const int LOGN=18;
int ctr = 1;
void dfs(int x, int p, int d){
st[x] = ctr++;
back[st[x]] = x;
depth[x] = d;
twok[x][0] = p;
for (int i = 1; i <= LOGN; i++){
if (twok[x][i-1] == -1) break;
twok[x][i] = twok[twok[x][i - 1]][i - 1]; // eg 16th parent = 8th parent 8th parent
}
for (int i = 0; i < adj[x].size(); i++){
if (adj[x][i] != p) dfs(adj[x][i], x, d + 1);
}
en[x] = ++ctr; // changed
back[en[x]] = x;
}
int kth_parent(int x, int k){
for (int i = 0; i <= LOGN; i++){
if (k & (1 << i)) x = twok[x][i];
if (x <= -1) return -1;
}
return x;
}
int lca(int x, int y){
if (depth[x] > depth[y]) {
int dd = depth[x] - depth[y];
x = kth_parent(x, dd);
} else {
int dd = depth[y] - depth[x];
y = kth_parent(y, dd);
}
if (x == y) return x;
for (int i = 17; i >= 0; i--){
if (twok[x][i] != twok[y][i]){
x = twok[x][i];
y = twok[y][i];
}
}
return twok[x][0];
}
int LCA[MAXN];
namespace tarjan {
int num[MAXN], low[MAXN], weight[MAXN], whichScc[MAXN];
bool instack[MAXN];
vector<int> adjlist[MAXN];
vector<int> sccadjlist[MAXN];
stack<int> nodes;
int ctr = 0;
int sccptr = 0;
void dfs(int x){
low[x] = num[x] = ctr++;
nodes.push(x);
instack[x] = true;
for (auto i : adjlist[x]){
if (num[i] == -1) dfs(i);
if (instack[i]) low[x] = min(low[i], low[x]);
}
if (low[x] == num[x]){
// loop through the nodes of the found SCC.
while (instack[x]){
int v = nodes.top(); nodes.pop();
instack[v] = false;
whichScc[v] = sccptr;
weight[sccptr]++;
}
sccptr++;
}
}
}
int32_t main()
{
IAMSPEED
cin >> n >> k;
FOR(i,1,n-1) {
int a,b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
FOR(i,1,n) cin >> C[i];
FOR(i,1,n) {
V[C[i]].pb(i);
}
memset(twok,-1,sizeof twok);
dfs(1,-1,0);
FOR(i,1,k) {
sort(all(V[i]),[](int x, int y) {
return st[x]<st[y];
});
}
FOR(i,1,k) {
LCA[i]=lca(V[i][0],V[i].back());
}
FOR(i,1,n) {
if(i == LCA[C[i]]){
} else {
if(twok[i][0] != -1) {
if(C[i] != C[twok[i][0]])tarjan::adjlist[C[i]].pb(C[twok[i][0]]);
}
}
}
memset(tarjan::num,-1,sizeof tarjan::num);
FOR(i,1,k) {
if(tarjan::num[i] == -1) tarjan::dfs(i);
}
FOR(i,1,k) {
for(auto j:tarjan::adjlist[i]) {
if(tarjan::whichScc[i] != tarjan::whichScc[j]) {
tarjan::sccadjlist[tarjan::whichScc[i]].pb(tarjan::whichScc[j]);
}
}
}
int ans = inf;
FOR(i,0,tarjan::sccptr-1) {
if(tarjan::sccadjlist[i].size() == 0){
chmin(ans, tarjan::weight[i]);
}
}
cout << --ans;
return (0-0) ;
}
컴파일 시 표준 에러 (stderr) 메시지
capital_city.cpp: In function 'void dfs(long long int, long long int, long long int)':
capital_city.cpp:63:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < adj[x].size(); i++){
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |