题目链接
因版权原因不予提供
分析
别看这是普及模拟赛,其实基本上是提高难度...像这题做NOIpT1的话也说的过去
有个很显然的暴力思路就是枚举c,a,b,时间复杂度\(O(N^3)\),
然后正解其实就是改变枚举顺序,我们先枚举a点,然后将所有可作为c点的点存起来,再从那些c点遍历得到可行b点统计答案,这样就不会重复且符合题意
不过这道题需要仔细读题,像我这种菜B一开始就理解错题意了
代码
#include <cstdio> #include <cstring> #include <cstring> #include <cctype> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <iostream> #define ll long long #define ri register int #define ull unsigned long long using namespace std; template <class T>inline void read(T &x){ x=0;int ne=0;char c; while(!isdigit(c=getchar()))ne=c=='-'; x=c-48; while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48; x=ne?-x:x;return ; } const int maxn=3005; const int inf=0x7fffffff; struct Edge{ int ne,to; }edge[maxn<<1],_edge[maxn<<1]; int h[maxn],num_edge=1; inline void add_edge(int f,int to){ edge[++num_edge].ne=h[f]; edge[num_edge].to=to; h[f]=num_edge; } int _h[maxn],_num_edge=1;//反向边 inline void _add_edge(int f,int to){ _edge[++_num_edge].ne=_h[f]; _edge[_num_edge].to=to; _h[f]=_num_edge; } int n,m,sc,sb; bool vis[maxn]; vector <int> g; void _dfs(int now,int fa){ int v;if(vis[now])return ; vis[now]=1;g.push_back(now); for(ri i=_h[now];i;i=_edge[i].ne){ v=_edge[i].to; if(v==fa)continue; _dfs(v,now); } return ; } void dfs(int now,int fa){ int v;if(vis[now])return ; sb++;vis[now]=1; for(ri i=h[now];i;i=edge[i].ne){ v=edge[i].to; if(v==fa)continue; dfs(v,now); } return ; } int main(){ int T,x,y,z; srand(19260817);//闷声发大财 预祝长者大寿 Long Live Jiang !!! read(T); while(T--){ ll ans=0; read(n),read(m); memset(h,0,sizeof(h)); memset(_h,0,sizeof(_h)); num_edge=_num_edge=1; for(ri i=1;i<=m;i++){ read(x),read(y); add_edge(x,y); _add_edge(y,x); } int ss=(n+1)*sizeof(bool); for(ri i=1;i<=n;i++){ memset(vis,0,ss); g.clear(); _dfs(i,0); memset(vis,0,ss); for(ri j=0;j<g.size();j++){ sb=0; dfs(g[j],0); ans+=sb; //printf("--%d %d %d\n",g[j],i,sb); } } printf("%lld\n",ans); } return 0; }
ZROI17普及23-A.如烟题解--技巧枚举
猜你喜欢
转载自www.cnblogs.com/Rye-Catcher/p/9458229.html
今日推荐
周排行