| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
高手帮我看看吧,WA了N次了,谢谢!!#include <stdio.h>
#include <iostream>
using namespace std;
struct edge{ //保存边的权值
__int64 time;
int start,end;
int cost;
};
edge *e;
struct node{ //储存边的邻接表
int id; //自己的ID
int adj; //第一个领结点的ID
node *next;
node(){
adj=0;
next=NULL;
}
node(int np,node *nt){
adj=np;
next=nt;
}
};
node *p[101000];
int n,k,sh,sc;
int mark[101000];//标记点是否被访问过。
int son[101000]; //保存节点的子节点个数
int *ans;
int dfs(node *tmp){ //深度优先遍历,获得当前节点和所有子节点的个数(子节点的个数+1)
mark[tmp->id]=1;
int id=tmp->id;
int res=1;
while(tmp){
node *adjnode=p[tmp->adj];
if(mark[adjnode->id]==0){
mark[adjnode->id]=1;
res+=dfs(adjnode);
}
tmp=tmp->next;
}
son[id]=res;
return res;
}
void addedge(int start,int end){ //向邻接表中添加节点
node *tmp=new node(end,p[start]);
p[start]=tmp;
p[start]->id=start;
//
}
int cmp(const void *a1,const void *a2){ //对边的下标排序
edge p1=e[*(int *)a1];
edge p2=e[*(int *)a2];
__int64 total1=p1.cost*p1.time;
__int64 total2=p2.cost*p2.time;
return (total1-total2);
}
int main(){
// freopen("data.txt","r",stdin);
while(scanf("%d%d%d%d",&n,&k,&sh,&sc)!=EOF){
int i,j;
e=new edge[n];
ans=new int[n+1];//用来保存边节点的下表,方便后面排序
for(i=0;i<=n;i++){
p[i]=NULL;
ans[i]=i;
}
for(i=1;i<n;i++){
cin>>e[i].start>>e[i].end>>e[i].cost;
e[i].time=0;
addedge(e[i].start,e[i].end);
addedge(e[i].end,e[i].start);
}
memset(mark,0,sizeof(mark));
memset(son,0,sizeof(son));
dfs(p[1]);
for(i=1;i<n;i++){
int s1=e[i].start;
int s2=e[i].end;
int min=son[s1]>son[s2]?son[s2]:son[s1];
e[i].time=2*(n-min)*min;//获得重复的次数
}
qsort(&ans[1],n-1,sizeof(ans[1]),cmp); //对下标排序
if(sh>sc){
for(i=1;i<=k;i++){
printf("%d ",ans[i]);
}
printf("\n");
}else{
for(i=n-1;i>=n-k;i--){
printf("%d ",ans[i]);
}
printf("\n");
}
}
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator