URAL 1272 Non-Yekaterinburg Subway (查并集)

来源:互联网 发布:江西先锋软件集团 编辑:程序博客网 时间:2024/06/14 19:12
#include<stdio.h>#define MAX_ISLANDS (10000 + 1)#define MAX_TUNELS (12000 + 1)#define MAX_BRIDGES (12000 + 1)struct CONNECTION{int oneIsland;int anotherIsland;};CONNECTION connectionArray[MAX_TUNELS + MAX_BRIDGES];int connectionNum;int islands;int tunels;int bridges;int belongTo[MAX_ISLANDS];int findBelongTo(int island){if (island == belongTo[island])return island;belongTo[island] = findBelongTo(belongTo[island]);return belongTo[island];}int connect(int oneIsland, int anotherIsland){int oneBelongTo = findBelongTo(oneIsland);int anotherBelongTo = findBelongTo(anotherIsland);if (oneBelongTo != anotherBelongTo){belongTo[oneBelongTo] = anotherBelongTo;return 1;}return 0;}int main(){scanf("%d %d %d", &islands, &tunels, &bridges);int island;for (island = 1; island <= islands; island++)belongTo[island] = island;int oneIsland, anotherIsland;int tunel;for (tunel = 1; tunel <= tunels; tunel++){scanf("%d %d", &oneIsland, &anotherIsland);connect(oneIsland, anotherIsland);}int minBridges = 0;int bridge;for (bridge = 1; bridge <= bridges; bridge++){scanf("%d %d", &oneIsland, &anotherIsland);if (connect(oneIsland, anotherIsland))minBridges++;}printf("%d", minBridges);return 0;}


                                             
0 0