2015百度之星程序设计大赛 - 热身赛

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

Harry and Magical Computer

 
 Accepts: 182
 
 Submissions: 653
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.

Input

There are several test cases, you should process to the end of file. For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. 1n100,1m10000 The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). 1a,bn

Output

Output one line for each test case. If the computer can finish all the process print "YES" (Without quotes). Else print "NO" (Without quotes).

Sample Input
3 2

3 1

2 1

3 3

3 2

2 1

1 3
Sample Output
YES

NO

有向图判环。拓扑排序搞一下即可。

 1 #include <iostream>

 2 #include <sstream>

 3 #include <ios>

 4 #include <iomanip>

 5 #include <functional>

 6 #include <algorithm>

 7 #include <vector>

 8 #include <string>

 9 #include <list>

10 #include <queue>

11 #include <deque>

12 #include <stack>

13 #include <set>

14 #include <map>

15 #include <cstdio>

16 #include <cstdlib>

17 #include <cmath>

18 #include <cstring>

19 #include <climits>

20 #include <cctype>

21 using namespace std;

22 #define XINF INT_MAX

23 #define INF 0x3FFFFFFF

24 #define MP(X,Y) make_pair(X,Y)

25 #define PB(X) push_back(X)

26 #define REP(X,N) for(int X=0;X<N;X++)

27 #define REP2(X,L,R) for(int X=L;X<=R;X++)

28 #define DEP(X,R,L) for(int X=R;X>=L;X--)

29 #define CLR(A,X) memset(A,X,sizeof(A))

30 #define IT iterator

31 typedef long long ll;

32 typedef pair<int,int> PII;

33 typedef vector<PII> VII;

34 typedef vector<int> VI;

35 vector<int>G[110];

36 int deg[110];

37 int main()

38 {

39     ios::sync_with_stdio(false);

40     int n,m;

41     while(cin>>n>>m){

42         int u,v;

43         for(int i=0;i<n;i++)G[i].clear();

44         for(int i=0;i<n;i++)deg[i]=0;

45         for(int i=0;i<m;i++){

46             cin>>u>>v;

47             u--;

48             v--;

49             G[v].PB(u);

50             deg[u]++;

51         }

52         queue<int>q;

53         for(int i=0;i<n;i++){

54             if(!deg[i])q.push(i);

55         }

56         int num=0;

57         while(!q.empty()){

58             int x= q.front();

59             q.pop();

60             num++;

61             for(int i=0;i<G[x].size();i++){

62                 int v=G[x][i];

63                 deg[v]--;

64                 if(deg[v]==0)q.push(v);

65             }

66         }

67         if(num==n)cout<<"YES"<<endl;

68         else cout<<"NO"<<endl;

69 

70     }

71     return 0;

72 }

Have meal

 
 Accepts: 256
 
 Submissions: 523
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

I have been in school for several years, so I have visited all messes here. Now I have lost intersts in all of the foods. So when during the meal time, I don’t know which mess I should go to. So I came up with a solution. There are 4 messes in our school, I number them from 0 to 3. Then I says “Big Bing Small Jiang, Point Who Is Who!”, when I say the first word I point to the mess which is numbered 0, when I say the i-th (i>1) word I point to the mess whose number is one larger than the previous one. In case of the number of previous mess is 3, I will point to 0 again. I will go to the mess which I point to last time. Thus in this case I will go to the mess which is numbered 3. The following table explains the course of my solution to this case.  Word I say Mess id I point to Big 0 Bing 1 Small 2 Jiang 3 Point 0 Who 1 Is 2 Who 3

I will go to university after several days, I have heard that there are so many messes in it. So I will apply my solution again. Surpose there are n messes which are numberd through 0 to n-1, and I will say m words. When I say the first word I point to the mess which is numbered 0, when I say the i-th (i>1) word I point to the mess whose number is one larger than the previous one. In case of the number of previous mess is n-1, I will point to 0 again. I will go to the mess which I point to last time. So which mess will I point to?. It is so time-consuming to count it through manual work. So I want you to write a program to help me. Would you help me?

Input

Multi test cases (about 10000), every case contain two integers n and m in a single line.

[Technical Specification] 1<=n, m<=100

Output

For each case, output the number of the mess which I should go to.

Sample Input
4 3

1 100
Sample Output
2

0
移动m-1次,问对应的下标是多少,直接输出(m-1)%n即可。。。不贴代码了

Cities

 
 Accepts: 35
 
 Submissions: 155
 Time Limit: 4000/2000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward JayYe because he beats the devil and save the princess.The king decide to give JayYe exactly K cities as his daughter's dowry. Here comes the question.Although JayYe beats the devil,his knee was injured.So he doesn't want to move too much,he wants his citys as close as possible,that is, JayYe wants the expected distance of his cities as small as possible. The expected distance is defined as the expected distance between node u and node v,both u and v are randomly choose from JayYe's K cities equiprobably(that means first choose u randomly from JayYe’s K cities,then choose v randomly from JayYe’s K cities,so the case u equals to v is possible). Suppose you are the king,please determine the K cities so that JayYe is happy. Because the author is lazy,you only need tell me the minimum expect distance.

Input

The first line contains a single integer T,indicating the number of test cases. Each test case begins with two integers n and K,indicating the number of cities in this country,the number of cities the king gives to the knight.Then follows n-1 lines,each line contains three integers a,b,and c, indicating there is a road connects city a and city b with length c.

[Technical Specification] 1 <= T <= 100 1 <= K <= min(50,n) 1 <= n <= 2000 1 <= a,b <= n 0 <= c <= 100000

Output

For each case, output one line, contain one integer, the minimum expect distance multiply K2.

Sample Input
1

2 2

1 2 1
Sample Output
2

 简单的树形dp加背包。

dp[i][j]表示以i为根节点的子树中取了j个点的最小的expect distance是多少。转移的时候,其实就是一个分组背包而已。

 1 #include <iostream>

 2 #include <sstream>

 3 #include <ios>

 4 #include <iomanip>

 5 #include <functional>

 6 #include <algorithm>

 7 #include <vector>

 8 #include <string>

 9 #include <list>

10 #include <queue>

11 #include <deque>

12 #include <stack>

13 #include <set>

14 #include <map>

15 #include <cstdio>

16 #include <cstdlib>

17 #include <cmath>

18 #include <cstring>

19 #include <climits>

20 #include <cctype>

21 using namespace std;

22 #define XINF INT_MAX

23 #define INF 0x3FFFFFFF

24 #define MP(X,Y) make_pair(X,Y)

25 #define PB(X) push_back(X)

26 #define REP(X,N) for(int X=0;X<N;X++)

27 #define REP2(X,L,R) for(int X=L;X<=R;X++)

28 #define DEP(X,R,L) for(int X=R;X>=L;X--)

29 #define CLR(A,X) memset(A,X,sizeof(A))

30 #define IT iterator

31 typedef long long ll;

32 typedef pair<int,ll> PII;

33 typedef vector<PII> VII;

34 typedef vector<int> VI;

35 ll dp[2010][60];

36 vector<PII>G[2010];

37 ll dp2[60];

38 int k;

39 void dfs(int u,int fa){

40     for(int i=0;i<G[u].size();i++){

41         PII p = G[u][i];

42         int v = p.first;

43         ll d = p.second;

44         if(v==fa)continue;

45         dfs(v,u);

46         for(int j=0;j<=k;j++)dp2[j]=dp[u][j];

47         for(int j=0;j<=k;j++){

48             for(int l=0;l<=j;l++){

49                 dp2[j]=min(dp2[j],dp[u][j-l]+dp[v][l]+d*l*(k-l)*2);

50             }

51         }

52         for(int j=0;j<=k;j++){

53             dp[u][j]=dp2[j];

54         }

55     }

56 }

57 

58 int main()

59 {

60     ios::sync_with_stdio(false);

61     int t;

62     cin>>t;

63     while(t--){

64         int n;

65         cin>>n>>k;

66         int u,v,d;

67         for(int i=0;i<n;i++)G[i].clear();

68         for(int i=0;i<n-1;i++){

69             cin>>u>>v>>d;

70             u--;

71             v--;

72             G[u].PB(MP(v,d));

73             G[v].PB(MP(u,d));

74         }

75         for(int i=0;i<n;i++){

76             for(int j=0;j<=k;j++){

77                 if(j<=1)dp[i][j]=0;

78                 else dp[i][j]=1LL<<60;

79             }

80         }

81         dfs(0,-1);

82         cout<<dp[0][k]<<endl;

83 

84     }

85 

86     return 0;

87 }

A card problem

 
 Accepts: 5
 
 Submissions: 23
 Time Limit: 5000/2500 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

JayYe is solving a card problem. His IQ is low recently, so he ask you for help. In a room, there are N tables numbered 1,2,3,,N. There is a blue card on each table. The ith card is on the ith table. On the ith blue card, there is a number Ai. Now you can put on a red card on each table and the number on the ith red card is Bi(1BiAi). Then a problem comes, for one arrangement how many pair of (i,j) that satisfy i<j and gcd(Bi,Bj) has at most one different prime factor? We call these pairs goodpairs. For example, 12 has two different prime factors. (12=223) But this is not you problem. You can easily know there are i=1NAidifferent arrangements. Your task is to calculate the sum of the number of goodpairs of all different arrangements. As the answer can be rather large, find remainder after dividing the number by 1000000007(109+7).

Input

There are several test cases. In each test case: The first line contains a integer N(1N100000). The second line contains N integers A1,A2,A3,...AN(1Ai100000,1iN).

Output

For each test case, output the remainder of division of the resulting number by 1000000007(109+7).

Sample Input
2

2 5

2

10 10
Sample Output
10

98
Hint
In the first test case, there are 10 different arrangements: {1,1} {1,2} {1,3} {1,4} {1,5} {2,1} {2,2} {2,3} {2,4} {2,5}. All pairs are good, so answer is 10.
 
这题暂时还不会,莫比乌斯没有学好,不会搞,留到暑假来补上。
 


 

你可能感兴趣的:(程序设计)