Codechef:Sereja and Arcs/SEAARC

传送门

题解:
这种题显然对出现次数分类讨论一下。

出现少的 O ( S 2 ) O(S^2) O(S2)做,出现多的 O ( n ) O(n) O(n)做,最后可以做到 O ( n n log ⁡ n ) O(n \sqrt{n \log n}) O(nnlogn )的复杂度。

#include <bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;
const int RLEN=1<<18|1;
inline char nc() {
   
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob) ? -1 : *ib++;
} 
inline int rd() {
   
	char ch=nc(); int i=0,f=1;
	while(!isdigit(ch)) {
   if(ch=='-')f=-1; ch=nc();}
	while(isdigit(ch)) {
   i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
	return i*f;
}

const int N=1e5+50, L=1e5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度优先遍历(DFS): ```c++ #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 typedef char VertexType; typedef int EdgeType; typedef struct { VertexType vexs[MAX_VERTEX_NUM]; EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; int vexnum, arcnum; } MGraph; bool visited[MAX_VERTEX_NUM]; void CreateGraph(MGraph* G) { printf("请输入顶点数和边数:"); scanf("%d %d", &(G->vexnum), &(G->arcnum)); printf("请输入顶点:"); for (int i = 0; i < G->vexnum; i++) { scanf(" %c", &(G->vexs[i])); } for (int i = 0; i < G->vexnum; i++) { for (int j = 0; j < G->vexnum; j++) { if (i == j) { G->arcs[i][j] = 0; } else { G->arcs[i][j] = INFINITY; } } } printf("请输入每条边的起点、终点、权值:\n"); for (int k = 0; k < G->arcnum; k++) { int i, j, w; scanf("%d %d %d", &i, &j, &w); G->arcs[i][j] = w; } } void DFS(MGraph* G, int v) { visited[v] = true; printf("%c ", G->vexs[v]); for (int j = 0; j < G->vexnum; j++) { if (G->arcs[v][j] != INFINITY && !visited[j]) { DFS(G, j); } } } void DFSTraverse(MGraph* G) { for (int i = 0; i < G->vexnum; i++) { visited[i] = false; } for (int i = 0; i < G->vexnum; i++) { if (!visited[i]) { DFS(G, i); } } } int main() { MGraph G; CreateGraph(&G); printf("深度优先遍历结果:"); DFSTraverse(&G); return 0; } ``` 广度优先遍历(BFS): ```c++ #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 typedef char VertexType; typedef int EdgeType; typedef struct { VertexType vexs[MAX_VERTEX_NUM]; EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; int vexnum, arcnum; } MGraph; bool visited[MAX_VERTEX_NUM]; void CreateGraph(MGraph* G) { printf("请输入顶点数和边数:"); scanf("%d %d", &(G->vexnum), &(G->arcnum)); printf("请输入顶点:"); for (int i = 0; i < G->vexnum; i++) { scanf(" %c", &(G->vexs[i])); } for (int i = 0; i < G->vexnum; i++) { for (int j = 0; j < G->vexnum; j++) { if (i == j) { G->arcs[i][j] = 0; } else { G->arcs[i][j] = INFINITY; } } } printf("请输入每条边的起点、终点、权值:\n"); for (int k = 0; k < G->arcnum; k++) { int i, j, w; scanf("%d %d %d", &i, &j, &w); G->arcs[i][j] = w; } } void BFS(MGraph* G, int v) { int queue[MAX_VERTEX_NUM], front = 0, rear = 0; printf("%c ", G->vexs[v]); visited[v] = true; queue[rear++] = v; while (front != rear) { int i = queue[front++]; for (int j = 0; j < G->vexnum; j++) { if (G->arcs[i][j] != INFINITY && !visited[j]) { printf("%c ", G->vexs[j]); visited[j] = true; queue[rear++] = j; } } } } void BFSTraverse(MGraph* G) { for (int i = 0; i < G->vexnum; i++) { visited[i] = false; } for (int i = 0; i < G->vexnum; i++) { if (!visited[i]) { BFS(G, i); } } } int main() { MGraph G; CreateGraph(&G); printf("广度优先遍历结果:"); BFSTraverse(&G); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值