T1
30pts
教训:存图双向边数组要开2倍(就是这么简单!)还害得我调了半个小时才发现,改后ac
code:
using namespace std;
int n,a,b,anode,bnode;
const int maxn = 1e6+10;
struct edge{
int to,next;
}e[maxn];
int nodeflag = -1;
int head[maxn],siz[maxn],cnt,ans[maxn];
void addedge(int u,int v){
cnt++;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
bool vis[maxn];
int dfs(int x,int fa){
vis[x] = true;
int size_ = 1;
for(int i = head[x];i;i = e[i].next){
if(!vis[e[i].to]) size_ += dfs(e[i].to,x);
}
if(size_ == a){
anode = x;
bnode = fa;
nodeflag = 1;
}
if(size_ == b){
anode = x;
bnode = fa;
nodeflag = 2;
}
return size_;
}
void dfs1(int x){
vis[x] = true;
ans[x] = a--;
for(int i = head[x];i;i = e[i].next){
if(!vis[e[i].to]) dfs1(e[i].to);
}
}
void dfs2(int x){
vis[x] = true;
ans[x] = b--;
ans[x] -= 2*ans[x];
for(int i = head[x];i;i = e[i].next){
if(!vis[e[i].to]) dfs2(e[i].to);
}
}
int main(){
cin>>n>>a>>b;
int u,v;
for(int i = 1;i < n;i++){
cin>>u>>v;
addedge(u,v);
addedge(v,u);
}
dfs(v,u);
if(anode == 0){
cout<<"-1";
return 0;
}
for(int i = 1;i <= n;i++) vis[i] = false;
if(nodeflag == 1){
vis[bnode] = true;
dfs1(anode);
dfs2(bnode);
}
else{
vis[anode] = true;
dfs1(bnode);
dfs2(anode);
}
for(int i = 1;i <= n;i++) cout<<ans[i]<<" ";
}
T2
0pts 赛后思路理清
T3
赛中骗分 15pts 赛后ac。还有一种做法,贴边走一定是最优的,把所有矩形抽象成左线段,只取经过x轴的矩形,从左到右连边跑最短路。
T4 脑裂待解