<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Cpp on Rain&#39;s Blog</title>
    <link>https://example.org/categories/cpp/</link>
    <description>Recent content from Rain&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en</language>
    
    <managingEditor>2385689419@qq.com (Rain)</managingEditor>
    <webMaster>2385689419@qq.com (Rain)</webMaster>
    
    <copyright>All articles on this blog are licensed under the BY-NC-SA license agreement unless otherwise stated. Please indicate the source when reprinting!</copyright>
    
    <lastBuildDate>Sat, 30 May 2026 19:37:52 +0800</lastBuildDate>
    
    
    <atom:link href="https://example.org/categories/cpp/index.xml" rel="self" type="application/rss&#43;xml" />
    

    
    

    <item>
      <title>cg 实验六</title>
      <link>https://example.org/post/cg-6/</link>
      <pubDate>Sat, 30 May 2026 19:37:52 &#43;0800</pubDate>
      <author>2385689419@qq.com (Rain)</author>
      <guid>https://example.org/post/cg-6/</guid>
      <description>
        <![CDATA[<h1>cg 实验六</h1><p>Author: Rain(2385689419@qq.com)</p>
        
          <h1 id="1547g-how-many-paths-题解c">
<a class="header-anchor" href="#1547g-how-many-paths-%e9%a2%98%e8%a7%a3c"></a>
1547G How Many Paths 题解（C++）
</h1><h2 id="题意重述">
<a class="header-anchor" href="#%e9%a2%98%e6%84%8f%e9%87%8d%e8%bf%b0"></a>
题意重述
</h2><p>给定一个有向图，起点固定为 <code>1</code>。</p>
<p>对于每个点 <code>v</code>，需要判断从 <code>1</code> 到 <code>v</code> 的路径数量属于下面哪一种：</p>
<ul>
<li><code>0</code>：一条路径也没有</li>
<li><code>1</code>：恰好一条路径</li>
<li><code>2</code>：路径多于一条，但总数有限</li>
<li><code>-1</code>：路径有无穷多条</li>
</ul>
<p><img src="/post/cg-6/images/example.png" alt="例图"></p>
<p>注意：</p>
<ul>
<li>图里允许有环</li>
<li>图里允许有自环</li>
<li>但不存在重边</li>
</ul>
<p>题目的难点不在于“数路径”，而在于区分：</p>
<ol>
<li>多条但有限</li>
<li>无穷多条</li>
</ol>
<p>这两类不能混在一起处理。</p>
<hr>
<h2 id="先想清楚为什么会出现无穷多条路径">
<a class="header-anchor" href="#%e5%85%88%e6%83%b3%e6%b8%85%e6%a5%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0%e6%97%a0%e7%a9%b7%e5%a4%9a%e6%9d%a1%e8%b7%af%e5%be%84"></a>
先想清楚：为什么会出现无穷多条路径？
</h2><p>只有一种原因：</p>
<blockquote>
<p>从 <code>1</code> 出发，能走进某个环，然后还能从这个环走到目标点。</p>
</blockquote>
<p>一旦满足这个条件，我们就可以在环里多绕任意多圈，再离开去目标点，于是路径数就是无穷。</p>
<p>所以整道题本质上分成两步：</p>
<ol>
<li>找出哪些点的答案一定是 <code>-1</code></li>
<li>对剩下的点统计“有限路径数量”，但只需要区分 <code>0 / 1 / 大于 1</code></li>
</ol>
<hr>
<h2 id="核心思路总览">
<a class="header-anchor" href="#%e6%a0%b8%e5%bf%83%e6%80%9d%e8%b7%af%e6%80%bb%e8%a7%88"></a>
核心思路总览
</h2><p>算法分成 4 步：</p>
<ol>
<li>从 <code>1</code> 出发做一次遍历，只保留 <strong>可达点</strong></li>
<li>在可达子图里做 <strong>强连通分量（SCC）分解</strong></li>
<li>找出所有“含环 SCC”，并从这些点继续向外传播，标记答案为 <code>-1</code></li>
<li>删除这些 <code>-1</code> 点后，剩余图一定是 DAG，在 DAG 上做一次路径 DP</li>
</ol>
<hr>
<h2 id="第一步只关心从-1-可达的部分">
<a class="header-anchor" href="#%e7%ac%ac%e4%b8%80%e6%ad%a5%e5%8f%aa%e5%85%b3%e5%bf%83%e4%bb%8e-1-%e5%8f%af%e8%be%be%e7%9a%84%e9%83%a8%e5%88%86"></a>
第一步：只关心从 1 可达的部分
</h2><p>很多环可能根本到不了，自然不会影响答案。</p>
        
        <hr><p>Published on 2026-05-30 at <a href='https://example.org/'>Rain's Blog</a>, last modified on 2026-05-30</p>]]>
      </description>
      
        <category>cpp</category><category>cg</category>
      
    </item>
    
  </channel>
</rss>
