# NOIP模拟题 2016.8.29 [树相关问题] [数论] [贪心] [拓扑排序]

A

7
1 2
1 3
1 4
1 5
1 6
1 7

0

6
1 2
2 3
2 4
5 4
6 4

4

B

3 3
1 2 1

2

7 10
1 3 3 1 2 3 1

6 2 3

C

5
2 2 1 1 3
1 5
2 5 1
2 5 4
1 5
0

7

A:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 205;
struct Edge
{
int to,next;
}edge[maxn<<1];
int maxedge;
{
edge[++maxedge] = (Edge) { v,head[u] };
edge[++maxedge] = (Edge) { u,head[v] };
}
{
int u,v;
int n;
void init()
{
scanf("%d",&n);
maxedge=-1;
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
}
}
int depth[maxn];
void dfs(int u,int father,int deep,int &end)
{
depth[u]=deep;
if(!end || depth[end]<depth[u]) end=u;
{
int v=edge[i].to;
dfs(v,u,deep+1,end);
}
}
void work()
{
int ans=0;
for(int i=1;i<n;i++)
{
int max1=0,max2=0;
int end1=0,end2=0;
dfs(end1,0,1,end2);
max1=depth[end2]-1;
end1=end2=0;
dfs(end1,0,1,end2);
max2=depth[end2]-1;
smax(ans,max1*max2);
}
printf("%d",ans);
}
int main()
{
freopen("A.input","r",stdin);
freopen("A.output","w",stdout);
init();
work();
return 0;
}


B:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int maxn = 100005;
LL n,k;
struct Data
{
int order;
LL val;
bool operator < (const Data t) const
{
if(val ^ t.val) return val < t.val;
return order < t.order;
}
}a[maxn];
bool cmp(const Data a,const Data b)
{
return a.order < b.order;
}
void init()
{
scanf(AUTO AUTO,&n,&k);
for(int i=1;i<=n;i++) scanf(AUTO,&a[i].val),a[i].order=i;
sort(a+1,a+n+1);
}
int ans[maxn],cnt;
void work()
{
LL last=0;
int now=1;
int tot=n;
while(now<=n && k>=(a[now].val-last)*tot)
{
k-=(a[now].val-last)*tot;
last = a[now].val;
while(a[now].val==last)
{
tot--;
if(now<n) now++;
else break;
}
}
sort(a+1,a+n+1,cmp);
last+=k/tot;
k%=tot;
for(int i=1;i<=n;i++) if(a[i].val>last) ans[++cnt]=i;
int pos=1+k;
for(int i=pos;i<cnt;i++) printf("%d ",ans[i]);
printf("%d",ans[cnt]);
for(int i=1;i<pos;i++) if(a[ans[i]].val>last+1) printf(" %d",ans[i]);
}
int main()
{
freopen("B.input","r",stdin);
freopen("B.output","w",stdout);
init();
work();
return 0;
}


C:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 205;
struct Edge
{
int to,next;
}edge[maxn*maxn];
int maxedge;
int in[maxn];
{
edge[++maxedge] = (Edge) { v,head[u] };
in[v]++;
}
int room[maxn];
int n;
void init()
{
scanf("%d",&n);
maxedge=-1;
for(int i=1;i<=n;i++) scanf("%d",room+i),room[i]--;
for(int i=1;i<=n;i++)
{
int cnt;
scanf("%d",&cnt);
for(int j=1;j<=cnt;j++)
{
int tmp;
scanf("%d",&tmp);
}
}
}
queue <int> que[3];
int deg[maxn];
int work(int S)
{
int ans=0;
int cnt=0;
memcpy(deg,in,sizeof(in));
for(int i=1;i<=n;i++) if(!in[i]) que[room[i]].push(i),cnt++;
int cur=S;
while(cnt)
{
while(!que[cur].empty())
{
int u=que[cur].front();que[cur].pop();cnt--;
{
int v=edge[i].to;
if(--deg[v]==0) que[room[v]].push(v),cnt++;
}
}
if(!cnt) break;
ans++; // goto another box
cur++;
if(cur>2) cur-=3;
}
return ans+n; // count the cost of every node
}
int main()
{
freopen("C.input","r",stdin);
freopen("C.output","w",stdout);
init();
int ans=INF;
for(int i=0;i<3;i++) smin(ans,work(i));
printf("%d",ans);
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：NOIP模拟题 2016.8.29 [树相关问题] [数论] [贪心] [拓扑排序] 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)