이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2")
#include<bits/stdc++.h>
//#define int long long
//#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out ","w",stdout)
#define pb push_back
#define vii vector<pii>
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf=1e9;
const int mod2=1e9+7;
const int mod1=998244353;
struct icd
{
int a,b;
string c;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c, d,e;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,l,l2,r2,dem=0;
int i,s10,s12;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
// fin(task);
//fou(task);
#endif
NHP
/// cin>>s1;
/// modwwe
phongbeo(),down
}
int dp[3001][2][2];
vector<int> v[3001];
id trace[1001][1001][2][2];
ie trace2[1001][1001][2][2];
int b[3001];
bool c[3001];
int a[3001];
bool de=0;
void dfs(int x,int y)
{
/// 0 0 no tat cac con no tat
/// 0 1 no tat cac con no bat
/// 1 0 no bat cac con no tat
/// 1 1 no va cac con bat
dp[x][b[x]][1]=0;
dp[x][1-b[x]][1]=inf;
dp[x][1-b[x]][0]=inf;
dp[x][b[x]][0]=0;
int ss=0;
for(auto f:v[x])
{
ss++;
if(f!=y)
{
dfs(f,x);
s2=dp[x][0][0];
s3=dp[x][0][1];
s4=dp[x][1][0];
s5=dp[x][1][1];
dp[x][0][0]=dp[x][0][0]+dp[f][0][0];
dp[x][1][0]=dp[x][1][0]+dp[f][0][0];
dp[x][0][1]=dp[x][0][1]+dp[f][1][0];
dp[x][1][1]=dp[x][1][1]+dp[f][1][0];
if(!c[f])
{
dp[x][0][0]=min({dp[x][0][0],s4+dp[f][1][1]+1});
dp[x][1][0]=min({dp[x][1][0],s2+dp[f][1][1]+1});
dp[x][0][1]=min({dp[x][0][1],s5+dp[f][0][1]+1});
dp[x][1][1]=min({dp[x][1][1],s3+dp[f][0][1]+1});
}
/// dpx00
{
if(dp[x][0][0]==s2+dp[f][0][0])
{
trace[ss][x][0][0]= {ss-1,x,0,0};
trace2[ss][x][0][0]= {v[f].size(),f,0,0,0};
}
else if(dp[x][0][0]==s4+dp[f][1][1]+1)
{
trace[ss][x][0][0]= {ss-1,x,1,0};
trace2[ss][x][0][0]= {v[f].size(),f,1,1,1};
}
}
///dpx10
{
if(dp[x][1][0]==s4+dp[f][0][0])
{
trace[ss][x][1][0]= {ss-1,x,1,0};
trace2[ss][x][1][0]= {v[f].size(),f,0,0,0};
}
else if(dp[x][1][0]==s2+dp[f][1][1]+1)
{
trace[ss][x][1][0]= {ss-1,x,0,0};
trace2[ss][x][1][0]= {v[f].size(),f,1,1,1};
}
}
///dpx01
{
if(dp[x][0][1]==s3+dp[f][1][0])
{
trace[ss][x][0][1]= {ss-1,x,0,1};
trace2[ss][x][0][1]= {v[f].size(),f,1,0,0};
}
else if(dp[x][0][1]==s5+dp[f][0][1]+1)
{
trace[ss][x][0][1]= {ss-1,x,1,1};
trace2[ss][x][0][1]= {v[f].size(),f,0,1,1};
}
}
/// dpx11
{
if(dp[x][1][1]==s5+dp[f][1][0])
{
trace[ss][x][1][1]= {ss-1,x,1,1};
trace2[ss][x][1][1]= {v[f].size(),f,1,0,0};
}
else if(dp[x][1][1]==s3+dp[f][0][1]+1)
{
trace[ss][x][1][1]= {ss-1,x,0,1};
trace2[ss][x][1][1]= {v[f].size(),f,0,1,1};
}
}
}
}
}
vector<int> vans;
void backtrack(int x,int y,int z,int f)
{
if(trace[x][y][z][f].b!=0)
backtrack(trace[x][y][z][f].a,trace[x][y][z][f].b,trace[x][y][z][f].c,trace[x][y][z][f].d);
if(trace2[x][y][z][f].e) vans.pb(trace2[x][y][z][f].b);
if(trace2[x][y][z][f].b!=0)
backtrack(trace2[x][y][z][f].a,trace2[x][y][z][f].b,trace2[x][y][z][f].c,trace2[x][y][z][f].d);
}
void case1()
{
/// no swap
for(int i=1; i<=n; i++)
b[i]=a[i];
// for(auto x:v[l])
// b[x]=1-b[x];
// for(auto x:v[r])
// b[x]=1-b[x];
dfs(1,0);
if(dp[1][0][0]<inf)
{de=1;
backtrack(v[1].size(),1,0,0);
}
else if(dp[1][1][1]<inf&&!c[1])
{de=1;
vans.pb(1);
backtrack(v[1].size(),1,1,1);
}
}
void case2()
{
/// swap left
for(int i=1; i<=n; i++)
b[i]=a[i];
for(auto x:v[l])
b[x]=1-b[x];
b[l]=1-b[l];
// for(auto x:v[r])
// b[x]=1-b[x];
dfs(1,0);
if(dp[1][0][0]<=inf)
{de=1;
vans.pb(l);
backtrack(v[1].size(),1,0,0);
}
else if(dp[1][1][1]<=inf&&!c[1])
{de=1;
vans.pb(l);
vans.pb(1);
backtrack(v[1].size(),1,1,1);
}
}
void case3()
{
/// swap right
for(int i=1; i<=n; i++)
b[i]=a[i];
// for(auto x:v[l])
// b[x]=1-b[x];
///b[l]=1-b[l];
for(auto x:v[r])
b[x]=1-b[x];
b[r]=1-b[r];
dfs(1,0);
if(dp[1][0][0]<=inf)
{de=1;
vans.pb(r);
backtrack(v[1].size(),1,0,0);
}
else if(dp[1][1][1]<=inf&&!c[1])
{de=1;
vans.pb(1);
vans.pb(r);
backtrack(v[1].size(),1,1,1);
}
}
void case4()
{
/// no swap
for(int i=1; i<=n; i++)
b[i]=a[i];
for(auto x:v[l])
b[x]=1-b[x];
b[l]=1-b[l];
for(auto x:v[r])
b[x]=1-b[x];
b[r]=1-b[r];
dfs(1,0);
if(dp[1][0][0]<=inf)
{de=1;
vans.pb(l);
vans.pb(r);
backtrack(v[1].size(),1,0,0);
}
else if(dp[1][1][1]<=inf&&!c[1])
{de=1;
vans.pb(l);
vans.pb(r);
vans.pb(1);
backtrack(v[1].size(),1,1,1);
}
}
void phongbeo()
{
cin>>n;
m=1;
for(int i=1; i<n; i++)
cin>>l>>r,v[l].pb({r}),
v[r].pb({l});
// cin>>l>>r;
// c[l]=1;
// c[r]=1;
for(int i=1; i<=m; i++)
{
for(int i=1; i<=n; i++)
cin>>a[i];
vans.clear();
de=0;
case1();
//if(!de)case2();
//if(!de) case3();
//if(!de)case4();
if(!de) cout<<"impossible",down
else
{
cout<<vans.size();
//<<" ";
// for(auto x:vans)
// cout<<x<<" ";
//down
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
50 | main()
| ^~~~
xanadu.cpp: In function 'void dfs(int, int)':
xanadu.cpp:106:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
106 | trace2[ss][x][0][0]= {v[f].size(),f,0,0,0};
| ~~~~~~~~~^~
xanadu.cpp:111:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
111 | trace2[ss][x][0][0]= {v[f].size(),f,1,1,1};
| ~~~~~~~~~^~
xanadu.cpp:119:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
119 | trace2[ss][x][1][0]= {v[f].size(),f,0,0,0};
| ~~~~~~~~~^~
xanadu.cpp:124:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
124 | trace2[ss][x][1][0]= {v[f].size(),f,1,1,1};
| ~~~~~~~~~^~
xanadu.cpp:132:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
132 | trace2[ss][x][0][1]= {v[f].size(),f,1,0,0};
| ~~~~~~~~~^~
xanadu.cpp:137:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
137 | trace2[ss][x][0][1]= {v[f].size(),f,0,1,1};
| ~~~~~~~~~^~
xanadu.cpp:147:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
147 | trace2[ss][x][1][1]= {v[f].size(),f,1,0,0};
| ~~~~~~~~~^~
xanadu.cpp:152:52: warning: narrowing conversion of 'v[f].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
152 | trace2[ss][x][1][1]= {v[f].size(),f,0,1,1};
| ~~~~~~~~~^~
xanadu.cpp: In function 'void phongbeo()':
xanadu.cpp:276:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
276 | for(int i=1; i<=n; i++)
| ^~~
xanadu.cpp:278:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
278 | vans.clear();
| ^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |