I solve the this problem by DFS. but it is time limit exceed. here is my code:
#include<stdio.h>
long int k1;
long int g[10001][10001]={0},cl[10001];
long int d,cu[10001];
int dfs();
void dfs_visit(int u);
int main()
{
long int u,r,j,a,y;
long int c,s,i,t;
scanf("%ld",&t);
for(i=1;i ...