Language: C

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
const int maxn=410000;
int lin[maxn],tlin[maxn];
int tnum=0,num;
bool vis[maxn];
int head[maxn],next[maxn],g[maxn*2],val[maxn*2];
int te[maxn*2],e[maxn*2],id[maxn*2];
int len[maxn];
int recu[maxn],recv[maxn];
bool flag;
bool islin[maxn],isedge[maxn*2];
int l[maxn],r[maxn];
void dfs(int u,int deep)
{
    vis[u]=true;
    len[u]=deep;
    for(int i=head[u];i!=-1;i=next[i])
    if(!vis[g[i]])
        dfs(g[i],deep+1);
}
void find(int u,int t)
{
    vis[u]=true;
    tnum++;
    tlin[tnum]=u;

    if(u==t)
    {
        for(int i=1;i<=tnum;i++)
        {
            lin[i]=tlin[i];
            islin[lin[i]]=true;
            e[i]=te[i];
            if(i>1)
            {
                isedge[e[i]]=true;
                isedge[e[i]^1]=true;
            }
        }
        num=tnum;
        flag=true;
        return;
    }
    for(int i=head[u];i!=-1;i=next[i])
    if(!vis[g[i]])
    {
        te[tnum+1]=i;
        find(g[i],t);
        if(flag)return;
    }

    tnum--;
}

int solve(int u)
{
    vis[u]=true;
    int tmp=0;
    for(int i=head[u];i!=-1;i=next[i])
    if(!vis[g[i]] && !isedge[i])
    {
        tmp=max(tmp,solve(g[i]))+1;
    }
    return tmp;
}
int main()
{
    int sec;
    scanf("%d",&sec);
    for(int z=1;z<=sec;z++)
    {
        int n;
        scanf("%d",&n);
        memset(next,-1,sizeof(next));
        int tot=0;
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[tot]=v;val[tot]=w;id[tot]=i;next[tot]=head[u];head[u]=tot++;
            g[tot]=u;val[tot]=w;id[tot]=i;next[tot]=head[v];head[v]=tot++;
            recu[i]=u;
            recv[i]=v;
        }
        memset(vis,false,sizeof(vis));
        dfs(1,0);
        int s,t;
        int maxt=0;
        for(int i=1;i<=n;i++)
        if(len[i]>maxt)
        {
            maxt=len[i];
            s=i;
        }
        memset(vis,false,sizeof(vis));
        dfs(s,0);
        maxt=0;
        for(int i=1;i<=n;i++)
        if(len[i]>maxt)
        {
            maxt=len[i];
            t=i;
        }
        //s,t
        flag=false;
        memset(vis,false,sizeof(vis));
        memset(islin,false,sizeof(islin));
        memset(isedge,false,sizeof(isedge));
        tnum=0;
        find(s,t);
        //ok num lin islin[]
        l[0]=0;
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=num;i++)
        {
            int tmp=solve(lin[i]);
            l[i]=max(l[i-1],i-1+tmp);
        }
        r[num+1]=0;
        memset(vis,false,sizeof(vis));
        for(int i=num;i>=1;i--)
        {
            int tmp=solve(lin[i]);
            r[i]=max(r[i+1],num-i+tmp);
        }
        //ans
        //road1
        int ans=0,rec=0;
        for(int i=2;i<=num;i++)
        {
            int a=val[i];
            int b=max(l[i-1],r[i]);
            if(a*b<ans)
            {
                ans=a*b;
                rec=id[i];
            }
            else
            if(a*b==ans && id[i]<rec)
            {
                rec=id[i];
            }
        }
        for(int i=1;i<n-1;i++)
        if(!isedge[i])
        {
            int a=val[i];
            int b=maxt;
             if(a*b<ans)
            {
                ans=a*b;
                rec=id[i];
            }
            else
            if(a*b==ans && id[i]<rec)
            {
                rec=id[i];
            }
        }
        printf("%d\n",rec);
    }
    return 0;
}

Compiler output:
main.c:1:9: fatal error: iostream: No such file or directory
    1 | #include<iostream>
      |         ^~~~~~~~~~
compilation terminated.