結果

問題 No.812 Change of Class
ユーザー asugen0402asugen0402
提出日時 2019-05-17 16:00:07
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 6,266 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 30,608 KB
実行使用メモリ 13,196 KB
最終ジャッジ日時 2023-09-03 15:22:59
合計ジャッジ時間 5,278 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
12,636 KB
testcase_01 AC 7 ms
9,420 KB
testcase_02 AC 16 ms
10,832 KB
testcase_03 AC 28 ms
13,196 KB
testcase_04 AC 27 ms
12,772 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
8,880 KB
testcase_08 AC 4 ms
8,816 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 10 ms
8,816 KB
testcase_12 WA -
testcase_13 AC 3 ms
8,872 KB
testcase_14 AC 4 ms
8,848 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 9 ms
8,888 KB
testcase_36 AC 5 ms
8,932 KB
testcase_37 AC 14 ms
8,928 KB
testcase_38 AC 6 ms
8,852 KB
testcase_39 AC 15 ms
8,884 KB
testcase_40 AC 7 ms
8,928 KB
testcase_41 AC 5 ms
8,856 KB
testcase_42 AC 27 ms
8,864 KB
testcase_43 AC 17 ms
8,920 KB
testcase_44 AC 19 ms
8,936 KB
testcase_45 AC 5 ms
8,908 KB
testcase_46 AC 15 ms
8,920 KB
testcase_47 AC 20 ms
8,848 KB
testcase_48 AC 25 ms
8,920 KB
testcase_49 AC 8 ms
8,852 KB
testcase_50 AC 4 ms
8,864 KB
testcase_51 AC 11 ms
8,928 KB
testcase_52 AC 19 ms
8,852 KB
testcase_53 AC 7 ms
8,852 KB
testcase_54 AC 7 ms
8,812 KB
testcase_55 AC 18 ms
8,908 KB
testcase_56 AC 20 ms
8,852 KB
testcase_57 AC 22 ms
8,900 KB
testcase_58 AC 13 ms
8,840 KB
testcase_59 AC 24 ms
8,884 KB
testcase_60 AC 4 ms
8,848 KB
testcase_61 AC 4 ms
8,916 KB
testcase_62 AC 4 ms
8,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_ON			1										// 汎用フラグ - ON
#define D_OFF			0										// 汎用フラグ - OFF
#define D_VTX_MAX		100000									// 最大頂点数
#define D_EDGE_MAX		100000									// 最大辺数

// 内部構造体 - 辺情報
typedef struct Edge {
	int miVNo;													// 接続先頂点 0~
	int miVSum;													// 接続頂点数合計
	int miLMax;													// 接続頂点最大距離
	struct Edge *mzpNext;										// 次の辺情報
} Edge;

// 内部構造体 - 頂点情報
typedef struct Vtx {
	Edge *mzpEdge;												// 辺
	int miDone;													// 処理済フラグ
	int miVSum;													// 接続頂点数合計
	int miLMax;													// 接続頂点最大距離
} Vtx;

// 内部変数
static FILE *szpFpI;											// 入力
static Vtx sz1Vtx[D_VTX_MAX];									// 頂点
static int siVCnt;												// 頂点数
static Edge sz1Edge[D_EDGE_MAX * 2];							// 辺
static int siECnt;												// 辺数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> 1行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 辺 - 追加
int
fAddEdge(
	int piVFNo					// <I> 頂点 - 元 0~
	, int piVTNo				// <I> 頂点 - 先 0~
)
{
	sz1Edge[siECnt].miVNo = piVTNo;
	sz1Edge[siECnt].mzpNext = sz1Vtx[piVFNo].mzpEdge;
	sz1Vtx[piVFNo].mzpEdge = &sz1Edge[siECnt];
	siECnt++;

	return 0;
}

// 辺 - 接続頂点数合計・接続頂点最大距離 - セット
int
fSetEdge(
	int piVNo					// <I> 頂点 0~
	, int *pipVSum				// <O> 接続頂点数合計
	, int *pipLMax				// <O> 接続頂点最大距離
)
{
	// 処理済フラグ
	if (sz1Vtx[piVNo].miDone != D_OFF) {
		return 0;
	}
	sz1Vtx[piVNo].miDone = D_ON;

	// 初期化
	*pipVSum = 0;
	*pipLMax = 0;

	// 辺でループ
	Edge *lzpEdge = sz1Vtx[piVNo].mzpEdge;
	while (lzpEdge != NULL) {

		// 下位へ
		fSetEdge(lzpEdge->miVNo, &lzpEdge->miVSum, &lzpEdge->miLMax);

		// 接続頂点数合計
		*pipVSum += lzpEdge->miVSum;

		// 接続頂点最大距離
		if (*pipLMax < lzpEdge->miLMax) {
			*pipLMax = lzpEdge->miLMax;
		}

		// 次の辺へ
		lzpEdge = lzpEdge->mzpNext;
	}

	// 自分を追加
	(*pipVSum)++;
	(*pipLMax)++;

	return 0;
}

// 頂点 - 接続頂点数合計・接続頂点最大距離 - セット
int
fSetVtx(
	int piVNo					// <I> 頂点 0~
	, int piVSum				// <I> 上側の接続頂点数合計
	, int piLMax				// <I> 上側の接続頂点最大距離
)
{
	// 処理済フラグ
	if (sz1Vtx[piVNo].miDone != D_ON) {
		return 0;
	}
	sz1Vtx[piVNo].miDone = D_OFF;

	// 辺でループ
	Edge *lzpEdge = sz1Vtx[piVNo].mzpEdge;
	int liLMax2 = 0;
	while (lzpEdge != NULL) {

		// 接続頂点数合計
		piVSum += lzpEdge->miVSum;

		// 接続頂点最大距離
		if (piLMax < lzpEdge->miLMax) {
			liLMax2 = piLMax;
			piLMax = lzpEdge->miLMax;
		}
		else if (liLMax2 < lzpEdge->miLMax) {
			liLMax2 = lzpEdge->miLMax;
		}

		// 次の辺へ
		lzpEdge = lzpEdge->mzpNext;
	}

	// 頂点 - 接続頂点数合計・接続頂点最大距離 - セット
	sz1Vtx[piVNo].miVSum = piVSum;
	sz1Vtx[piVNo].miLMax = piLMax;

	// 自分を追加
	piVSum++;
	piLMax++;
	liLMax2++;

	// 辺でループ
	lzpEdge = sz1Vtx[piVNo].mzpEdge;
	while (lzpEdge != NULL) {

		// 接続頂点最大距離
		int liLMax;
		if (lzpEdge->miLMax == piLMax - 1) {
			liLMax = liLMax2;
		}
		else {
			liLMax = piLMax;
		}

		// 下位へ
		fSetVtx(lzpEdge->miVNo, piVSum - lzpEdge->miVSum, liLMax);

		// 次の辺へ
		lzpEdge = lzpEdge->mzpNext;
	}

	return 0;
}

// 実行メイン
int
fMain(
)
{
	int i, liWork, liWork2;
	char lc1Buf[1024];

	// 頂点数・辺数 - 取得
	int liECnt;
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &siVCnt, &liECnt);

	// 辺 - 取得
	for (i = 0; i < liECnt; i++) {
		int liVNo1, liVNo2;
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d%d", &liVNo1, &liVNo2);
		liVNo1--;
		liVNo2--;

		// 辺 - 追加
		fAddEdge(liVNo1, liVNo2);
		fAddEdge(liVNo2, liVNo1);
	}

	// 辺 - 接続頂点数合計・接続頂点最大距離 - セット
	for (i = 0; i < siVCnt; i++) {
		if (sz1Vtx[i].miDone == D_OFF) {
			fSetEdge(i, &liWork, &liWork2);
		}
	}

	// 頂点 - 接続頂点数合計・接続頂点最大距離 - セット
	for (i = 0; i < siVCnt; i++) {
		if (sz1Vtx[i].miDone == D_ON) {
			fSetVtx(i, 0, 0);
		}
	}

	// クエリ数 - 取得
	int liQCnt;
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &liQCnt);

	// クエリ - 取得
	for (i = 0; i < liQCnt; i++) {
		int liVNo;
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d", &liVNo);
		liVNo--;

		// 日数
		int liDCnt;
		int liVal = 1;
		for (liDCnt = 0; ; liDCnt++) {
			if (sz1Vtx[liVNo].miLMax <= liVal) {
				break;
			}
			else {
				liVal *= 2;
			}
		}

		// 出力
		sprintf(lc1Buf, "%d %d\n", sz1Vtx[liVNo].miVSum, liDCnt);
		fOut(lc1Buf);
	}

	return 0;
}

// 1回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// データ - 初期化
	memset(sz1Vtx, 0, sizeof(sz1Vtx));							// 頂点
	memset(sz1Edge, 0, sizeof(sz1Edge));						// 辺
	siECnt = 0;													// 辺数

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}

0