2017icpc 乌鲁木齐网络赛

A .Banana

Bananas are the favoured food of monkeys.

In the forest, there is a Banana Company that provides bananas from different places.

The company has two lists.

The first list records the types of bananas preferred by different monkeys, and the second one records the types of bananas from different places.

Now, the supplier wants to know, whether a monkey can accept at least one type of bananas from a place.

Remenber that, there could be more than one types of bananas from a place, and there also could be more than one types of bananas of a monkey’s preference.

Input Format

The first line contains an integer TT, indicating that there are TT test cases.

For each test case, the first line contains two integers NN and MM, representing the length of the first and the second lists respectively.

In the each line of following NN lines, two positive integers i, ji,j indicate that the ii-th monkey favours the jj-th type of banana.

In the each line of following MM lines, two positive integers j, kj,k indicate that the jj-th type of banana could be find in the kk-th place.

All integers of the input are less than or equal to 5050.

Output Format

For each test case, output all the pairs x, yx,y that the xx-the monkey can accept at least one type of bananas from the yy-th place.

These pairs should be outputted as ascending order. That is say that a pair of x, yx,y which owns a smaller xx should be output first.

If two pairs own the same xx, output the one who has a smaller yy first.

And there should be an empty line after each test case.

样例输入


6 4 
1 1 
1 2 
2 1 
2 3 
3 3 
4 1 
1 1 
1 3 
2 2 
3 3 
样例输出

1 1 
1 2 
1 3 
2 1 
2 3 
3 3 
4 1 
4 3


签到,写两个map记录每个产地有哪几种水果,每个猴子喜欢哪种水果,双重for一下,第三重用map迭代器迭代下各产地的各个水果种类是否能有对应猴子喜欢就行。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 using namespace std;
 7 map<int,bool>::iterator it;
 8 int main()
 9 {
10     int n,m,T,u,v;
11     scanf("%d",&T);
12     while(T--)
13     {
14         scanf("%d%d",&n,&m);
15         map<int,bool> a[60],b[60];
16         for(int i=1;i<=n;i++)
17         {
18             scanf("%d%d",&u,&v);
19             a[u][v]=1;
20         }
21         for(int j=1;j<=m;j++)
22         {
23             scanf("%d%d",&u,&v);
24             b[v][u]=1;
25         }
26         for(int i=1;i<=50;i++)
27         {
28             for(int j=1;j<=50;j++)
29             {
30                 for(it=b[j].begin();it!=b[j].end();it++)
31                 {
32                     if(a[i].find(it->first)!=a[i].end())
33                     {
34                         printf("%d %d
",i,j);
35                         break;
36                     }
37                 }
38             }
39         }
40         printf("
");
41     }
42     return 0;
43 }
View Code

C.Coconut

Coconut is Captain Gangplank’s favourite fruit. That is why he needs to drink coconut juice from bb coconuts each day.

On his next trip, he would pass through NN citis.

His trip would begin in the 11-st city and end in the NN-th city.

The journey from the ii-th city to the (i+1)(i+1)-th city costs D_iD 
​i 
​​ days.

Initially, there is no coconut on his ship. Fortunately, he could get supply of C_iC 
​i 
​​ coconuts from the ii-th city.

Could you tell him, whether he could drink coconut juice every day during the trip no not?

Input Format

The first line contains an integer TT, indicating that there are TT test cases.

For each test case the first line contains two integers NN and bb as described above.

The second line contains NN integers C_1, C_2, cdots, C_NC 
​1 
​​ ,C 
​2 
​​ ,⋯,C 
​N 
​​ .

The third line contains N-1N−1 integers D_1, D_2, cdots, D_{N-1}D 
​1 
​​ ,D 
​2 
​​ ,⋯,D 
​N−1 
​​ .

All integers in the input are less than 10001000.

Output Format

For each case, output Yes if Captain Gangplank could drink coconut juice every day, and otherwise output No.

样例输入


4 1 
3 2 1 4 
1 2 3 
4 2 
2 4 6 8 
3 2 1 
样例输出

Yes 
No


水题,for一遍就好了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int sum[1005];
 5 
 6 int main() {
 7     int n, b;
 8     int  t;
 9     scanf("%d", &t);
10     while(t --) {
11         scanf("%d%d", &n, &b);
12         memset(sum, 0, sizeof(sum));
13         for(int i = 1; i <= n; ++ i) {
14             int x;
15             scanf("%d", &x);
16             sum[i] = sum[i-1] + x;
17         }
18         int a = 0;
19         bool ok = true;
20         for(int i = 2; i <= n; ++ i) {
21             int x;
22             scanf("%d", &x);
23             a += x*b;
24             if(sum[i-1] < a) {
25                 ok = false;
26             }
27         }
28         if(ok) {
29             puts("Yes");
30         }
31         else {
32             puts("No");
33         }
34     }
35     return 0;
36 }
View Code

E. Half-consecutive Numbers


emmm就是打个表找找规律再打个更大的表的事情嘛~,如果嫌麻烦直接上eois233333。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define LL long long
 5 #define mod 1000000007
 6 using namespace std;
 7 LL a[23]={    0, 1, 8, 49, 288, 1681, 9800, 57121, 332928, 1940449, 11309768, 65918161, 384199200, 2239277041, 13051463048, 76069501249, 443365544448, 2584123765441, 15061377048200, 87784138523761, 511643454094368, 2982076586042449, 17380816062160328};
 8 int main()
 9 {
10     int T;
11     LL n;
12     scanf("%d",&T);
13     for(int kase=1;kase<=T;kase++)
14     {
15         scanf("%lld",&n);
16         for(int i=0;i<23;i++)
17         if(a[i]>=n)
18         {
19             printf("Case #%d: %lld
",kase,a[i]);
20             break;
21         }
22     }
23     return 0;
24 }
View Code

F. Islands


是道图论题- -,没有找到题面。队友写的我就不写题解了。

 1 #include <iostream>  
 2 #include <cstring>  
 3 #include <cstdio>  
 4 #define MAXN 100010 
 5 #define MAXE 100010
 6 using namespace std;  
 7 int head[MAXN],tot1,tot2;  
 8 struct Edge{  
 9     int u,v,next;  
10 }e1[MAXE],e2[MAXN];  
11 void addEdge(int u,int v,Edge* edge,int& tol){  
12     edge[tol].u=u;edge[tol].v=v;  
13     edge[tol].next=head[u];head[u]=tol++;  
14 }  
15 int n,m;  
16 int low[MAXN],dfn[MAXN],stack[MAXN],belong[MAXN],num[MAXN];  
17 bool instack[MAXN];  
18 int scc,top,Index;  
19 void Tarjan(int u){  
20     int v;  
21     low[u]=dfn[u]=++Index;  
22     stack[top++]=u;  
23     instack[u]=true;  
24     for(int i=head[u];i!=-1;i=e1[i].next){  
25         v=e1[i].v;  
26         if(!dfn[v]){  
27             Tarjan(v);  
28             if(low[u]>low[v]) low[u]=low[v];  
29         }  
30         else if(instack[v]&&low[u]>dfn[v])  
31             low[u]=dfn[v];  
32     }  
33     if(low[u]==dfn[u]){  
34         ++scc;  
35         do{  
36             v=stack[--top];  
37             instack[v]=false;  
38             belong[v]=scc;  
39             num[scc]++;  
40         }while(u!=v);  
41     }  
42 }  
43 int inde[MAXN],outde[MAXN];  
44 void solve(){  
45     memset(dfn,0,sizeof(dfn));  
46     memset(instack,false,sizeof(instack));  
47     memset(num,0,sizeof(num));  
48     scc=top=Index=0;  
49     for(int i=1;i<=n;++i)  
50         if(!dfn[i]) Tarjan(i);  
51     tot2=0;memset(head,-1,sizeof(head));  
52     memset(inde,0,sizeof(inde));  
53     memset(outde,0,sizeof(outde));  
54     int u,v;  
55     for(int i=0;i<m;++i){  
56         u=belong[e1[i].u];  
57         v=belong[e1[i].v];  
58         if(u!=v){  
59             addEdge(u,v,e2,tot2);  
60             inde[v]++;  
61             outde[u]++;  
62         }  
63     }  
64     int a=0,b=0;  
65     for(int i=1;i<=scc;++i){  
66         if(!inde[i]) a++;  
67         if(!outde[i]) b++;  
68     }  
69     if(scc==1)printf("0
");///特殊情况当图本身为强联通图时,输出0  
70     else  
71         printf("%d
",max(a,b));  
72 }  
73 int main()  
74 {  
75     int T;
76     scanf("%d
",&T);
77     while(T--){  
78         scanf("%d%d",&n,&m);
79         tot1=0;memset(head,-1,sizeof(head));  
80         int u,v;  
81         for(int i=0;i<m;++i){  
82             scanf("%d%d",&u,&v);  
83             addEdge(u,v,e1,tot1);  
84         }  
85         solve();  
86     }  
87     return 0;  
88 }  
View Code

 G. Query on a string

题目大意

给一个字符串S(长度最大100000)和T(最长10) 
给出n次操作,一共有两种操作:

    1. Q i j :计算S中区间[i,j]中有多少个子区间能和T匹配
    2. C x ch :将S[x]变成字符c

S中每个字符开头的|T|长度的字符串若是T串则为1,不是则为0。用vis数组记录,并且树状数组记录vis的前缀和。

预处理暴力匹配求vis数组和前缀和,一遇到不匹配的就break掉。

修改的话就检查包含修改位置的字符串上开头位置字符串是否和T匹配就好了。询问直接树状数组求和。

我可能因为边界问题T了。。看了网上AC代码跟我差不多。。我就明白我是边界的问题了。

  1 #include<bits/stdc++.h>
  2 #define clr(x) memset(x,0,sizeof(x))
  3 #define clr_1(x) memset(x,-1,sizeof(x))
  4 #define LL long long
  5 #define mod 1000000007
  6 using namespace std;
  7 const int N=1e5+10;
  8 char s[N],t[N];
  9 int bit[N];
 10 bool vis[N];
 11 int n,m,T,u,v,lens,lent;
 12 char c,cc;
 13 bool flag;
 14 void add(int i,int x)
 15 {
 16     while(i<=lens)
 17     {
 18         bit[i]+=x;
 19         i+= i & -i;
 20     }
 21     return ;
 22 }
 23 int sum(int i)
 24 {
 25     int s=0;
 26     while(i>0)
 27     {
 28         s+=bit[i];
 29         i-=i & -i;
 30     }
 31     return s;
 32 }
 33 int main()
 34 {
 35     scanf("%d",&T);
 36     while(T--)
 37     {
 38         clr(bit);
 39         clr(vis);
 40         scanf("%d",&n);
 41         scanf("%s",s+1);
 42         scanf("%s",t+1);
 43         lens=strlen(s+1);
 44         lent=strlen(t+1);
 45         for(int i=1;i<=lens-lent+1;i++)
 46         {
 47             flag=0;
 48             for(int j=1;j<=lent;j++)
 49                 if(s[i+j-1]!=t[j])
 50                 {
 51                     flag=1;
 52                     break;
 53                 }
 54             if(!flag)
 55             {
 56                 add(i,1);
 57                 vis[i]=1;
 58             }
 59         }
 60         for(int i=1;i<=n;i++)
 61         {
 62             scanf(" %c",&c);
 63             if(c=='Q')
 64             {
 65                 scanf("%d%d",&u,&v);
 66                 if(v-lent+1>u-1)
 67                     printf("%d
",sum(v-lent+1)-sum(u-1));
 68                 else
 69                     printf("0
");
 70             }
 71             if(c=='C')
 72             {
 73                 scanf("%d %c",&u,&cc);
 74                 swap(cc,s[u]);
 75                 for(int j=max(u-lent+1,1);j<=min(u,lens-lent+1);j++)
 76                 {
 77                     if(vis[j]==1)
 78                     {
 79                         if(cc!=s[u])
 80                         {
 81                             vis[j]=0;
 82                             add(j,-1);
 83                         }
 84                     }
 85                     else
 86                     {
 87                         flag=0;
 88                         for(int k=1;k<=lent;k++)
 89                             if(s[j+k-1]!=t[k])
 90                             {
 91                                 flag=1;
 92                                 break;
 93                             }
 94                         if(!flag)
 95                         {
 96                             add(j,1);
 97                             vis[j]=1;
 98                         }
 99                     }
100                 }
101             }
102         }
103         printf("
");
104     }
105     return 0;
106 }
View Code

H: Skiing

 time limit 1000ms memory limit 131072KB
 
i i
In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort has M different ski paths and N different flags situated at those turning points. The i-th path from the S -th flag to the T -th flag has length L . Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly. An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag. Now, you should help Bob find the longest available ski trail in the ski resort. Input Format The first line contains an integer T, indicating that there are T cases. In each test case, the first line contains two integers N and M where 0 < N ≤ 10000 and 0 < M ≤ 100000 as described above. Each of the following M lines contains three integers S , T , and L  (0 < L < 1000) describing a path in the ski resort. Output Format For each test case, ouput one integer representing the length of the longest ski trail. Sample Input
1 5 4 1 3 3 2 3 4 3 4 1 3 5 2
Sample Output
6


emmm就是找个最长路嘛- -,写个dfs+dp就行了0 0

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 10005
 6 
 7 struct edeg{
 8     int u, w, pre;
 9 };
10 
11 int dp[N];
12 int h[N];
13 edeg e[N*10];
14 bool vis[N];
15 
16 int DFS(int u);
17 void slove();
18 void init(){
19     memset(dp, -1, sizeof(dp));
20         memset(h, -1, sizeof(h));
21         
22 }
23 
24 int main() {
25     int t;
26     scanf("%d", &t);
27     while(t --) {
28         slove();
29     }
30     return 0;
31 }
32 
33 int DFS(int u) {
34     if(dp[u] != -1) return dp[u];
35     if(h[u] == -1) return 0;
36     for(int i = h[u]; ~i; i = e[i].pre) {
37         dp[u] = max(dp[u], DFS(e[i].u)+e[i].w);
38     }
39     return dp[u];
40 }
41 
42 void slove(){
43     int res = 0;
44         init();
45         int n, m;
46         int x, y, w;
47         scanf("%d%d", &n, &m);
48         for(int i = 1; i <= m; ++ i) {
49             scanf("%d%d%d", &x, &y, &w);
50             e[i] = edeg{y, w, h[x]};
51             h[x] = i;
52         }
53         for(int i = 1; i <= n; ++ i) {
54             if(h[i] != -1) {
55                 dp[i] = DFS(i);
56                 res = max(dp[i], res);
57             } 
58         }
59         printf("%d
", res);
60 }
View Code

菜校弱队在E题上卡了很久,然后又G胡改KMP最后还是没有发现边界有问题,止步于5题无缘去新疆吃哈密瓜了QAQ。

虽然6题也无缘233333。

很气,叉腰。

原文地址:https://www.cnblogs.com/wujiechao/p/7499234.html