Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #define lenght 15
- char Name[lenght][lenght];
- int Path[lenght][lenght];
- int visit[lenght];
- int Node, Edge;
- int Len;
- int Case;
- int stringCompare(char st1[], char st2[])
- {
- int i;
- for (i = 0; st1[i] && st2[i]; i++) {
- if (st1[i] != st2[i])
- return 0;
- }
- if (st1[i] != st2[i])
- return 0;
- return 1;
- }
- void stringCopy(char dest[], char src[])
- {
- int i;
- for (i = 0; src[i]; i++)
- dest[i] = src[i];
- dest[i] = '\0';
- }
- int searchString(char st[]){
- int i;
- for (i = 0; i < Len; i++){
- if (stringCompare(st, Name[i])){
- return i;
- }
- }
- return -1;
- }
- void readCase()
- {
- int i;
- int x, y;
- char name1[lenght];
- char name2[lenght];
- Len = 0;
- for (i = 0; i < Edge; i++) {
- scanf("%s %s", name1, name2);
- x = searchString(name1);
- if (-1 == x) {
- stringCopy(Name[Len], name1);
- x = Len;
- Len++;
- }
- y = searchString(name2);
- if (-1 == y) {
- stringCopy(Name[Len], name2);
- y = Len;
- Len++;
- }
- Path[x][y] = 1;
- }
- }
- void initPath(){
- int i, j;
- for (i = 0; i < Node; i++)for (j = 0; j < Node; j++){
- if (i == j)
- Path[i][j] = 1;
- else
- Path[i][j] = 0;
- }
- }
- void findAllPath()
- {
- int i, j, k;
- for (k = 0; k < Node; k++){
- for (i = 0; i < Node; i++){
- for (j = 0; j < Node; j++){
- if (Path[i][k] && Path[k][j]){
- Path[i][j] = 1;
- }
- }
- }
- }
- }
- void initvisit(){
- int i;
- for (i = 0; i < Node; i++){
- visit[i] = 0;
- }
- }
- void printCycle()
- {
- int i, j;
- if (Case > 1){
- printf("\n");
- }
- printf("Cycle are for Case#%d\n", Case++);
- initvisit();
- for (i = 0; i < Node; i++){
- if (0 == visit[i]){
- printf("%s", Name[i]);
- visit[i] = 1;
- for (j = 0; j < Node; j++){
- if (!visit[j] && Path[i][j] && Path[j][i]){
- printf(", %s", Name[j]);
- visit[j] = 1;
- }
- }
- printf("\n");
- }
- }
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- Case = 1;
- while (scanf("%d %d", &Node, &Edge)){
- if (0 == Node && 0 == Edge)break;
- initPath();
- readCase();
- findAllPath();
- printCycle();
- }
- }
Add Comment
Please, Sign In to add comment