Problem 1 Graph (graph.cpp/c/pas)

 

【题目描述】

给出 N 个点,M 条边的有向图,对于每个点 v,求 A(v) 表示从点 v 出发,能到达的编号最大的点。

 

【输入格式】

 1 行,2 个整数 N,M 接下来 M 行,每行 2 个整数 Ui,Vi,表示边 ⟨Ui,Vi⟩。点用 1,2,...,N 编号。

 

【输出格式】

N 个整数 A(1),A(2),...,A(N)。

 

【样例输入】

4 3

1 2

2 4

4 3

【样例输出】

4 4 3 4

【数据范围】

对于 60% 的数据,1 ≤ N,K ≤ 10^3

对于 100% 的数据,1 ≤ N,M ≤ 10^5。

Problem 2 Incr(incr.cpp/c/pas)

 

【题目描述】

数列 A1,A2,...,AN,修改最少的数字,使得数列严格单调递增。

 

【输入格式】

1 行,1 个整数 N

2 行,N 个整数 A1,A2,...,AN

【输出格式】

1 个整数,表示最少修改的数字

 

【样例输入】

3

1 3 2

【样例输出】

1

【数据范围】

对于 50% 的数据,N ≤ 10^3

对于 100% 的数据,1 ≤ N ≤ 10^5,1 ≤ Ai ≤ 10^9

Problem 3 Permutation (permutation.cpp/c/pas)

 

【题目描述】

1 到 N 任意排列,然后在排列的每两个数之间根据他们的大小关系插入“>”和“<”。

问在所有排列中,有多少个排列恰好有K个“<”。

例如排列(3, 4, 1, 5, 2)

3 < 4 > 1 < 5 > 2

共有2个“<”

 

【输入格式】

N,K

 

【输出格式】

答案

 

【样例输入】

5 2

【样例输出】

66

【数据范围】

20%:N <= 10

50%:答案在0..2^63-1内

100%:K < N <= 100


NOIP复习题旨在复习知识点,故写详细一点啦

T1:

强联通分量缩点的裸题(我当时竟然以为第一题应该不会太难而没考虑环,结果40分炸掉QAQ)

我用的是Kosaraj+dfs缩点,效率很低

不过还是写一下吧,反正我不会tarjan~

先是两遍深搜获取基本信息,然后再一遍深搜把各个缩点连接起来

最后一遍拓扑排序。

总共四边搜索,四次清空bool数组,三个邻接表

程序太垃圾啦~

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<vector>
  6 #define MAXN 100005
  7 using namespace std;
  8 vector<int> G[MAXN];
  9 vector<int> rG[MAXN];
 10 vector<int> nG[MAXN];
 11 vector<int> vs;
 12 int cmp[MAXN],cnt;
 13 int m[MAXN];
 14 int b[MAXN];
 15 int ru[MAXN];
 16 int V,E;
 17 void dfs1(int x){
 18     b[x]=1;
 19     for(int i=0;i<G[x].size();i++){
 20         int y=G[x][i];
 21         if(!b[y]){
 22             dfs1(y);
 23         }
 24     }
 25     vs.push_back(x);
 26 }
 27 void dfs2(int x){
 28     b[x]=1;
 29     cmp[x]=cnt;
 30     m[cnt]=max(m[cnt],x);
 31     for(int i=0;i<rG[x].size();i++){
 32         int y=rG[x][i];
 33         if(!b[y]){
 34             dfs2(y);
 35         }
 36     }
 37 }
 38 void suo(int x){
 39     b[x]=1;
 40     for(int i=0;i<G[x].size();i++){
 41         int y=G[x][i];
 42         if(cmp[x]!=cmp[y]){
 43             nG[cmp[x]].push_back(cmp[y]);
 44             ru[cmp[y]]++;
 45         }
 46         if(!b[y]){
 47             suo(y);
 48         }
 49     }
 50 }
 51 void topoSort(int x){
 52     b[x]=1;
 53     for(int i=0;i<nG[x].size();i++){
 54         int y=nG[x][i];
 55         if(!b[y]){
 56             topoSort(y);
 57         }
 58         m[x]=max(m[x],m[y]);
 59     }
 60 }
 61 int main()
 62 {
 63 //    freopen("data.in","r",stdin);
 64     scanf("%d%d",&V,&E);
 65     for(int i=1;i<=E;i++){
 66         int x,y;
 67         scanf("%d%d",&x,&y);
 68         G[x].push_back(y);
 69         rG[y].push_back(x);
 70     }
 71     memset(b,0,sizeof(b));
 72     for(int i=1;i<=V;i++){
 73         if(!b[i]){
 74             dfs1(i);
 75         }
 76     }
 77     memset(b,0,sizeof(b));
 78     for(int i=vs.size()-1;i>=0;i--){
 79         cnt++;
 80         int x=vs[i];
 81         if(!b[x]){
 82             dfs2(x);
 83         }
 84     }
 85     memset(b,0,sizeof(b));
 86     for(int i=1;i<=V;i++){
 87         if(!b[i]){
 88             suo(i);
 89         }
 90     }
 91     memset(b,0,sizeof(b));
 92     for(int i=1;i<=cnt;i++){
 93         if(!ru[i]){
 94             topoSort(i);
 95         }
 96     }
 97     for(int i=1;i<=V;i++){
 98         printf("%d ",m[cmp[i]]);
 99     }
100     return 0;
101 }
Code1

相关文章:

  • 2021-11-02
  • 2022-12-23
  • 2022-02-14
  • 2021-11-14
  • 2022-12-23
  • 2022-02-23
  • 2021-09-09
  • 2021-05-13
猜你喜欢
  • 2021-05-02
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
  • 2022-01-10
  • 2022-01-23
  • 2022-12-23
相关资源
相似解决方案