农民代表
- 积分
- 6241
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-12-27
- 最后登录
- 1970-1-1
|
蛋碎了一地。。。虽然好歹被我堆出来了,怎么看都不像个O(n+m)的算法,晚上睡觉电脑放客厅里面运行,早上看结果。。。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 875714
struct graph {
int v;
struct graph * next;
};
struct graph_array {
struct graph * p;
bool visited;
int leader;
int f_time;
};
void read_graph(struct graph_array g[]);
void generate_reverse(struct graph_array g[], struct graph_array g_r[]);
int find_f_time(struct graph_array g[], int vertex, int t);
void find_leader(struct graph_array g[], int vertex, int source);
int main(void)
{
struct graph_array g[MAX+1] = {{NULL, false, 0, 0}};
struct graph_array g_r[MAX+1] = {{NULL, false, 0, 0}};
struct graph * temp;
int counter[MAX+1] = {0};
int i, j, max_scc, index;
int time = 0;
read_graph(g);
generate_reverse(g, g_r);
for (i = MAX; i >= 1; i--)
{
if (g_r.visited == false)
time = find_f_time(g_r, i, time);
}
for (i = MAX; i >= 1; i--)
for (j = 1; j <= MAX; j++)
if ((g_r[j].f_time == i) && (g[j].visited == false))
find_leader(g, j, j);
for (i = 1; i <= MAX; i++)
counter[g[i].leader]++;
for (i = 1; i < 6; i++)
{
for (j = 1; j <= MAX; j++)
if (counter[j] > max_scc)
{
max_scc = counter[j];
index = j;
}
printf("max%d: %d\n", i, max_scc);
}
for (i = 1; i <= MAX; i++)
printf("%d\n", counter[i]);
printf("\n");
for (i = 1; i <= MAX; i++)
{
temp = g_r[i].p;
while (temp)
{
printf("%d ", i);
printf("%d\n", temp->v);
temp = temp->next;
}
}
printf("\n");
for (i = 1; i <= MAX; i++)
printf("%d\n", g_r[i].f_time);
printf("\n");
for (i = 1; i <= MAX; i++)
printf("%d\n", g[i].leader);
return 0;
}
void read_graph(struct graph_array g[])
{
FILE * fin = fopen("SCC.txt", "r");
struct graph * current, * temp;
int node;
int link;
while (fscanf(fin, "%d", &node) == 1)
{
fscanf(fin, "%d", &link);
if (g[node].p == NULL)
{
g[node].p = (struct graph *) malloc(sizeof(struct graph));
g[node].p->next = NULL;
g[node].p->v = link;
}
else
{
current = g[node].p;
while (current->next != NULL)
current = current->next;
temp = (struct graph *) malloc(sizeof(struct graph));
temp->next = NULL;
temp->v = link;
}
}
fclose(fin);
}
void generate_reverse(struct graph_array g[], struct graph_array g_r[])
{
int i;
struct graph *current, * temp, * create;
for (i = 1; i <= MAX; i++)
{
current = g[i].p;
while (current)
{
if (g_r[current->v].p == NULL)
{
g_r[current->v].p = (struct graph *) malloc(sizeof(struct graph));
g_r[current->v].p->next = NULL;
g_r[current->v].p->v = i;
}
else
{
temp = g_r[current->v].p;
while (temp->next != NULL)
temp = temp->next;
create = (struct graph *) malloc(sizeof(struct graph));
create->next = NULL;
temp->next = create;
}
current = current->next;
}
}
}
int find_f_time(struct graph_array g[], int vertex, int t)
{
int time = t;
struct graph * temp = g[vertex].p;
g[vertex].visited = true;
while (temp)
{
if (g[temp->v].visited == false)
find_f_time(g, temp->v, time);
temp = temp->next;
}
time++;
g[vertex].f_time = time;
return time;
}
void find_leader(struct graph_array g[], int vertex, int source)
{
struct graph * temp = g[vertex].p;
g[vertex].leader = source;
while (temp)
{
if (g[temp->v].visited == false)
find_leader(g, temp->v, source);
temp = temp->next;
}
}
[/i][/i][/i][/i][/i][/i] |
|